let func = function() {};
func.name
""
"func"
"anonymous"
undefined
var DOLPHIN = "\ud83d\udc2c";
DOLPHIN.length + [...DOLPHIN].length
2
3
4
var sym1 = Symbol(),
sym2 = Symbol();
var o1 = {},
o2 = {};
Object.defineProperties(o2, {
[sym1]: { value: "Sym 1", enumerable: true },
[sym2]: { value: "Sym 2"}
});
Object.assign(o1, o2);
Object.getOwnPropertySymbols(o1).length + Object.getOwnPropertySymbols(o2).length;
1
2
3
4
var obj1 = { ["__proto__"]: "FOO" };
var obj2 = { __proto__: "FOO" };
var obj3 = { "__proto__": "FOO" };
[
obj1.hasOwnProperty("__proto__"),
obj2.hasOwnProperty("__proto__"),
obj3.hasOwnProperty("__proto__")
]
[true, true, true]
[true, false, false]
[true, false, true]
[false, false, false]
[false, true, true]
- thrown
TypeError
function foo (a, b) {
"use strict";
var arrow = () => { return arguments[0]; };
return arrow(b);
};
foo("A", "B")
"A"
"B"
- thrown
TypeError
var f = () => { foo: "BAR" };
typeof f();
"string"
"object"
"undefined"
[
String.raw`Line\nTerminator` == "Line\\nTerminator",
`\u3042` == "\u3042",
String.raw`\u3042` == "\u3042",
]
[true, true, true]
[false, true, true]
[true, true, false]
[false, true, false]
function foo () {
return "out";
}
var obj = {
foo(n) {
if (n) { return "in"; }
return foo(1) + "side";
}
};
obj.foo();
"outside"
"inside"
- thrown
InternalError
: too much recursion
var f = () => { foo: function(){ return "FOO" } };
typeof f();
"object"
"function"
"undefined"
- thrown
SyntaxError
"use strict";
var f = () => { foo: function foo(){ return "FOO" } };
typeof f();
"object"
"function"
"undefined"
- thrown
SyntaxError
var obj = {
init() {
this.name = "obj";
},
Foo() {
this.init();
}
};
obj.Foo.prototype = {
init() {
this.name = "Foo";
},
};
var foo = new obj.Foo();
[
obj.name, foo.name
];
["obj", undefined]
["Foo", undefined]
[undefined, "Foo"]
- thrown
SyntaxError
- thrown
TypeError
Thanks: https://twitter.com/ziyunfei/status/571704467130736640
(function(arg1, ...rest) {
arg1 = 10;
return arguments[0];
}(1,2,3));
1
10
- thrown
SyntaxError
- thrown
TypeError
Thanks: https://twitter.com/ziyunfei/status/571828310759510016
console.log([
1 in Array(3),
1 in new Array(3),
1 in Array.from(Array(3)),
1 in Array(3).fill(),
1 in [...[,,,]]
]);
[false, true, true, false, true]
[false, false, false, false, false]
[false, false, true, true, true]
Thanks: https://twitter.com/ziyunfei/status/571831478323056640
var result = [];
function findFirstUndefined(value, index) {
if (value === undefined) {
result.push(index);
return true;
}
}
[1, , 3].some(findFirstUndefined);
[1, , 3].find(findFirstUndefined);
console.log(result);
[1, 1]
[]
[1]
- thrown
TypeError
Thanks: https://twitter.com/ziyunfei/status/572262011003789312
function getPrototypeChainOf(obj) {
var prototypeChain = [];
while (obj = Object.getPrototypeOf(obj)) {
prototypeChain.push(obj);
}
return prototypeChain;
}
var g = function *(){};
getPrototypeChainOf(g()).length;
2
3
4
5
var list = ["1", NaN, 3, "a"];
var sum = (c,p)=> c+p;
[
list.map(Number.isNaN).reduce(sum),
list.map(isNaN).reduce(sum)
];
[1, 1]
[1, 2]
[2, 1]
[2, 2]
var obj = {
length: 3,
*[Symbol.iterator](){
yield "A";
yield "B";
}
};
Array.from(obj).length;
2
3
- thrown
TypeError
I tried Q.11 in Traceur, but i got [ undefined, 'Foo' ], seems different with the Answer