http://ourjs.com/detail/52fb82e13bd19c4814000001
["1", "2", "3"].map(parseInt)
你实际上得到的应该是 [1, NaN, NaN] 因为 parseInt 需要两个参数 (val, radix) 但 map 传了 3 个 (element, index, array)
[typeof null, null instanceof Object]
typeof 对原生非可调用对象会始终返回 "object"
[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow)] ]
根据规范: 在一个空数组上应用reduce会抛初始化错误的异常 TypeError
var val = 'smtg';
console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');
它实际上会打印 'Something' 这个 + 操作符的优先级实际上比三元操作符要高.
var name = 'World!';
(function () {
if (typeof name === 'undefined') {
var name = 'Jack';
console.log('Goodbye ' + name);
} else {
console.log('Hello ' + name);
}
})();
var 声明的作用域在整个 function 中, 但并没有初始化
var END = Math.pow(2, 53);
var START = END - 100;
var count = 0;
for (var i = START; i <= END; i++) {
count++;
}
console.log(count);
这段代码会进入死循环, 2^53 是javascript中最大的数字, 2^53+1 与 2^53 等同, 因此 i 永远也不会比这个数大
var ary = [0,1,2];
ary[10] = 10;
ary.filter(function(x) { return x === undefined;});
Array.prototype.filter 不会应用到缺少的元素上
var two = 0.2
var one = 0.1
var eight = 0.8
var six = 0.6
[two - one == one, eight - six == two]
JavaScript 没有精确的数字, 即便它看上去有时侯能正常工作
function showCase(value) {
switch(value) {
case 'A':
console.log('Case A');
break;
case 'B':
console.log('Case B');
break;
case undefined:
console.log('undefined');
break;
default:
console.log('Do not know!');
}
}
showCase(new String('A'));
switch 使用 === 来枚举,并且 new String(x) !== x
function showCase2(value) {
switch(value) {
case 'A':
console.log('Case A');
break;
case 'B':
console.log('Case B');
break;
case undefined:
console.log('undefined');
break;
default:
console.log('Do not know!');
}
}
showCase(String('A'));
String(x) 不会返回一个 object 但会返回一个 string, 例如 typeof String(1) === "string"
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);
Infinity % 2 返回 NaN, -9 % 2 返回 -1
parseInt(3, 8)
parseInt(3, 2)
parseInt(3, 0)
3 在2进制中不存在, 很显然结果是NaN, 但0呢? parseInt 猜测你的意思是10, 所有返回是3.
Array.isArray( Array.prototype )
Array.prototype 是一个 Array.
var a = [0];
if ([0]) {
console.log(a == true);
} else {
console.log("wut");
}
[0] 被认为是真的,但跟 true 又不等同
[]==[]
== 很邪恶
'5' + 3
'5' - 3
Strings 知道怎么用+, 但是遇到-会被转化为数字
1 + - + + + - + 1
很有意思吧?(我也不清楚)
var ary = Array(3);
ary[0]=2
ary.map(function(elem) { return '1'; });
结果是["1", undefined * 2], 因为map 只能被初始化过的数组成员调用
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, 在javascript中变量中 arguments 是个对象,所以arguments 和局部变量所引用的内容是一样的。 注* 使用 use strict 可避免这种情况,参见:为什么使用"use strict"可以节约你的时间
var a = 111111111111111110000,
b = 1111;
a + b;
不精确的JavaScript数字即会影响小数,也会影响大数
Number.MIN_VALUE > 0
Number.MIN_VALUE 是最小的比0大的数, -Number.MAX_VALUE 可能会返回给你一个最大的负整数
[1 < 2 < 3, 3 < 2 < 1]
隐式转换
2 == [[[2]]]
每一个对象都被转换成了string,最终成了 "2"
3.toString()
3..toString()
3...toString()
3.x "3" 带上尾数xtoString是合法的, 但空字符串不是
(function(){
var x = y = 1;
})();
console.log(y);
console.log(x);
y自动被声明成全局变量, 不在function的域里面.
var a = /123/, b = /123/;
a == b
a === b
根据规范:正则表达式不能比较,因为每个正则都是唯一的。
var a = [1, 2, 3],
b = [1, 2, 3],
c = [1, 2, 4]
a == b
a === b
a > c
a < c
数组通过>和<会安顺序比较, 但==和===不会;
var a = {}, b = Object.prototype;
[a.prototype === b, Object.getPrototypeOf(a) === b]
Functions 有一个 prototype 属性,但是其它对象没有,所以 a.prototype 是 undefined.
每个 Object 有一个内部的属性可通过Object.getPrototypeOf 访问
function f() {}
var a = f.prototype, b = Object.getPrototypeOf(f);
a === b
f.prototype 是任何被创建出来对象的父对象, 但 new f 会返回 Object.getPrototypeOf 继承的父对象
function foo() { }
var oldName = foo.name;
foo.name = "bar";
[oldName, foo.name]
name 只读属性. 但是赋值时为什么不会报错呢?我不知道。
"1 2 3".replace(/\d/g, parseInt)
String.prototype.replace 实现上每组传入的参数为 1, 0, 2, 2, 3, 4.
function f() {}
var parent = Object.getPrototypeOf(f);
f.name // ?
parent.name // ?
typeof eval(f.name) // ?
typeof eval(parent.name) // ?
function 原型对象被定义在其它地方, 有名字, 可以被执行, 但不在当前的作用域中
var lowerCaseOnly = /^[a-z]+$/;
[lowerCaseOnly.test(null), lowerCaseOnly.test()]
参数被会转换成字符, 因此参数为 "null" 和 "undefined".
[,,,].join(", ")
JavaScript在数组中允许最后一个为逗号,所以原来的那个数组定义了3个 undefined
var a = {class: "Animal", name: 'Fido'};
a.class
答案是: 输出需要看是什么浏览器 class 是保留字, 在chrome Firefox 和 Opera中可作为属性名, 但IE不行. 另一方面他们都可以接受其他保留字 (例如 int, private, throws) 作为变量,但class不行.