javascript-puzzlers.herokuapp 刷题笔记
[1]
parseInt(string, radix) 将string当做一个radix进制的数,转化为10进制的数.
array.map(function(item, index, array))
所以当array.map(parseInt) parseInt拿到的radix 是 0,1,2,3等等
[“1”, “2”, “3”].map(parseInt) = [parseInt(‘1’, 0), parseInt(‘2’, 1), parseInt(‘3’, 2)] = [1, NAN, NAN]
[2]
null 是一种对象 所以 typeof null 会返回 ‘object’
但是 instanceof 只对对象的实例判读为true 如 {} instanceof Object === false
const a = {}
a instanceof Object === true
[3]
Math.pow(x,y)求x的y次方
array.reduce(function(initOrPrevReturn, arrayItem, index, array), init)
reduce的第一个参数是一个函数,函数的第一个参数初始是init,如果没有设置init则为array的第0项,arrayItem,和index表示当前function迭代的是array中的哪一项,当设置了init时,index从0开始,arrayItem也从第0项开始,当init没有设置时,array的第0项用做init,index从1开始,arrayItem也从array[1]开始.function执行的次数由array的长度决定,当array.length === 1 且未设置init时,function就不会被执行,直接返回array[0].当array.length === 0时,必须设置init 否则会报错
[4]
js中的 + 操作符的优先级 高于 ?:三元操作符 所以
var val = ‘smtg’;
console.log(‘Value is ’ + (val === ‘smtg’) ? ‘Something’ : ‘Nothing’);
会先执行’Value is ’ + (val === ‘smtg’) 然后被 ? 判断为true 返回 ‘Something’
[5]
var name = ‘World!’;
(function () {
if (typeof name === ‘undefined’) {
var name = ‘Jack’;
console.log(‘Goodbye ’ + name);
} else {
console.log(‘Hello ’ + name);
}
})();
var变量的声明会被提到函数作用域最上面 但是赋值操作不会 所以上面的代码等于如下
var name = ‘World!’;
(function () {
var name
if (typeof name === ‘undefined’) {
name = ‘Jack’;
console.log(‘Goodbye ’ + name);
} else {
console.log(‘Hello ’ + name);
}
})();
[6]
var END = Math.pow(2, 53);
var START = END - 100;
var count = 0;
for (var i = START; i <= END; i++) {
count++;
}
console.log(count);
it goes into an infinite loop, 2^53 is the highest possible number in javascript, and 2^53+1 gives 2^53, so i can never become larger than that.
这个知识点,我也很无奈。。。
[7]
var ary = [0,1,2];
ary[10] = 10;
ary.filter(function(x) { return x === undefined;});
Array.prototype.filter is not invoked for the missing elements
filter中的函数不会被缺失项调用,于是尝试得到结果
ary = [1,NaN,undefined,null,false,0]
ary.filter(x => x)
结果是[1]
可见,NaN,undefined,null 甚至false都不会被filter中的函数调用
[8]
var two = 0.2
var one = 0.1
var eight = 0.8
var six = 0.6
[two - one == one, eight - six == two]
结果是[true, false]
js没有精确的数学计算,浮点运算容易错
[9]
switch case 用===做严格比较,所以new String(x) !== x
[10]
String(x) does not create an object but does return a string, i.e. typeof String(1) === “string”
[11]
function isOdd(num) {
return num % 2 == 1;
}
function isEven(num) {
return num % 2 == 0;
}
function isSane(num) {
return isEven(num) || isOdd(num);
}
var values = [7, 4, ‘13’, -9, Infinity];
values.map(isSane); === [true, true, true, false, false]
Infinity % 2 gives NaN, -9 % 2 gives -1 (modulo operator keeps sign so it’s result is only reliable compared to 0)
[12]
parseInt(3, 8)
parseInt(3, 2)
parseInt(3, 0)
3 doesn’t exist in base 2, so obviously that’s a NaN, but what about 0? parseInt will consider a bogus radix and assume you meant 10, so it returns 3.
[13]
Array.prototype is an Array
[14]
var a = [0];
if ([0]) {
console.log(a == true);
} else {
console.log(“wut”);
}
[0] as a boolean is considered true. Alas, when using it in the comparisons it gets converted in a different way and all goes to hell.
在if中 数组长度大于0 就会认为是true,在 == 判断中 [0] == false;[1] == true
[15]
[]==[]
flase
== is the spawn of satan.
没有搞明白 但是 两个等号不能用于判断数据相等
[16]
‘5’ + 3
‘5’ - 3
‘53’ 2
Strings know about + and will use it, but they are ignorant of - so in that case the strings get converted to numbers.
能不转类型时 就不会转类型
[17]
1 + - + + + - + 1
2
原式 = 1 + (- + + + - + 1)
[19]
function sidEffecting(ary) {
ary[0] = ary[2];
}
function bar(a,b,c) {
c = 10
sidEffecting(arguments);
return a + b + c;
}
bar(1,1,1)
21
The result is 21, in javascript variables are tied to the arguments object so changing the variables changes arguments and changing arguments changes the local variables even when they are not in the same scope.
修改函数的参数变量 会改变arguments
[20]
var a = 111111111111111110000,
b = 1111;
a + b;
111111111111111110000
Lack of precision for numbers in JavaScript affects both small and big numbers.
111111111111111110000 + 1 也是111111111111111110000
var aba = 111111111111111110000
aba - 8192
111111111111111110000
aba - 8193
111111111111111100000
不知道为啥。。。
[22]
Number.MIN_VALUE > 0 true
Number.MIN_VALUE is the smallest value bigger than zero, -Number.MAX_VALUE gets you a reference to something like the most negative number.
[24]
2 == [[[2]]]
true
both objects get converted to strings and in both cases the resulting string is “2”
[25]
3.toString()
3..toString()
3…toString()
error “3” error
3.x is a valid syntax to define “3” with a mantissa of x, toString is not a valid number, but the empty string is.
toString()不是一个数字 所以第一个错
3..被解析成3.”. 是合法的 转换成字符串是“3”
3…无法解析
[26]
(function(){
var x = y = 1;
})();
console.log(y);
console.log(x);
1 error
y是全局变量 x是函数级作用域变量
[27]
var a = /123/,
b = /123/;
a == b
a === b
false false
ab都是正则表达式对象 对象比较是比地址 不相等
[28]
var a = [1, 2, 3],
b = [1, 2, 3],
c = [1, 2, 4]
a == b
a === b
a > c
a < c
false*3 true
数组可以用字典序比较 但是数组是对象 无法用===判断
[29]
var a = {}, b = Object.prototype;
[a.prototype === b, Object.getPrototypeOf(a) === b]
[false, true]
Functions have a prototype property but other objects don’t so a.prototype is undefined.
Every Object instead has an internal property accessible via Object.getPrototypeOf
[30]
function f() {}
var a = f.prototype, b = Object.getPrototypeOf(f);
a === b
false
f.prototype is the object that will become the parent of any objects created with new f while Object.getPrototypeOf returns the parent in the inheritance hierarchy
[31]
function foo() { }
var oldName = foo.name;
foo.name = “bar”;
[oldName, foo.name]
[‘foo’,’foo’]
name is a read only property. Why it doesn’t throw an error when assigned, I do not know.
[32]
“1 2 3”.replace(/\d/g, parseInt)
“1 NaN 3”
parseInt(str, radix)radix表示进制
replace(regexp|substr,newSubstr|function)
function(match,p1,p2,p3…,offset,string)
match 匹配到的子串
p1p2p3对应正则中()的部分
offset 匹配到子串的偏移量
string 被匹配的原串
所以 “1 2 3”.replace(/\d/g, parseInt) 执行了三次parseInt
参数分别是 [1,0] [2,2] [3,4]
[34]
var lowerCaseOnly = /^[a-z]+$/;
[lowerCaseOnly.test(null), lowerCaseOnly.test()]
[true,true]
the argument is converted to a string with the abstract ToString operation, so it is “null” and “undefined”.
[35]
[,,,].join(“,空格”)
“,空格,空格”
[].join(“, ) => “”
[1,].join(“, ) => “1”
数组的最后一个逗号后没有东西,就等于没有这个逗号(但数组长度已经确定) 就不会用到join设置的间隔符号 所以[,,,]的长度是3 而不是4
[37]
var a = new Date(“epoch”)
You get “Invalid Date”, which is an actual Date object (a instanceof Date is true). But invalid. This is because the time is internally kept as a Number, which in this case it’s NaN.
[38]
var a = Function.length,
b = new Function().length
a === b
false
It’s false. Function.length is defined to be 1. On the other hand, the length property of the Function prototype object is defined to be 0.
[40]
var min = Math.min(), max = Math.max()
min < max
false
min没有参数时返回Infinity max没有参数返回-Infinity
[41]
function captureOne(re, str) {
var match = re.exec(str);
return match && match[1];
}
var numRe = /num=(\d+)/ig,
wordRe = /word=(\w+)/i,
a1 = captureOne(numRe, “num=1”),
a2 = captureOne(wordRe, “word=1”),
a3 = captureOne(numRe, “NUM=2”),
a4 = captureOne(wordRe, “WORD=2”);
[a1 === a2, a3 === a4]
true false
忽略大小写的问题
[42]
var a = new Date(“2014-03-19”),
b = new Date(2014, 03, 19);
[a.getDay() === b.getDay(), a.getMonth() === b.getMonth()]
false false
b = new Date(2014, 03, 19); 是4月19号
[43]
if (‘http://giftwrapped.com/picture.jpg‘.match(‘.gif’)) {
‘a gif file’
} else {
‘not a gif file’
}
String.prototype.match silently converts the string into a regular expression, without escaping it, thus the ‘.’ becomes a metacharacter matching ‘/’.
match会将参数强转正则 .匹配换行符意外任何东西,所以.gif匹配到了域名中的/gif
[44]
function foo(a) {
var a;
return a;
}
function bar(a) {
var a = ‘bye’;
return a;
}
[foo(‘hello’), bar(‘hello’)]
[hello, bye]
foo中 声明的a已经存在 所以声明语句被删除
bar中 声明语句同样被删除但是赋值语句有效