最近发现一些好玩的JAVASCRIPT特性,其实也是es3的一些细小的东西,写出来给自己记录一下。
首先是Dmitry Soshnikov大牛的10道测试题,看了之后,发现自己还真有好多玩意不知道,还需要继续努力啊。
1. What’s the result of:
typeof typeof(null)
“undefined”
SyntaxError
“string”
“object”
TypeError
2. Are the algorithms of the following checks completely equvalent?
typeof foo == 'undefined'
and
typeof foo === 'undefined'
Yes
No
3. What’s the result of:
100['toString']['length']
100
3
1
8
0
SyntaxError
4. What’s the result of:
var a = (1,5 - 1) * 2
0.999999999
1
0.5
8
-0.5
4
5. What’s the result of:
var x = 10;
var foo = {
x: 20,
bar: function () {
var x = 30;
return this.x;
}
};
console.log(
foo.bar(),
(foo.bar)(),
(foo.bar = foo.bar)(),
(foo.bar, foo.bar)()
);
20, 20, 20, 20
20, 20, 10, 10
20, 20, 20, 10
20, 20, 10, 30
20, 10, 10, 10
20, 10, 30, SyntaxError
6. What’s the result of:
function f(x, y) {
x = 10;
console.log(
arguments[0],
arguments[1]
);
}
f();
10, null
10, undefined
undefined, undefined
10, NaN
10, 10
7. What’s the result of:
var
b = 10,
c = (
20,
function (x) { return x + 100},
function () { return arguments[0]}
);
a = b + c
({x: 10}).x
30, 10
20
10
30, 110
NaN, 10
8. What’s the result of:
1..z
SyntaxError
New Range object (equivalent to new Range(1, ‘z’)) including all numbers and letters
undefined
Error of Range object (incompatible types for range: number and string)
ReferenceError “z” is not defined
9. What’s the result of:
({
x: 10,
foo: function () {
function bar() {
console.log(x);
console.log(y);
console.log(this.x);
}
with (this) {
var x = 20;
var y = 30;
bar.call(this);
}
}
}).foo();
20, 30, 20
10, 30, 10
20, undefined, 10
undefined, 30, 20
SyntaxError
20, 30, 10
10. What’s the result of:
foreach (k in {a: 10, b: 20})
{
// ...
}
Always SyntaxError
ReferenceError or possibly no error
Always ReferenceError
Iteration over the object values
Iteration over the object keys
TypeError or possibly SyntaxError
相信不是大牛的话,即使看到正确答案也有很多不解,毕竟,人家大神说了,
if some questions seem to be hard, you can use console (that’s not a cheating, because some questions are hard).
详细解答,可以参看大神给出的解释页面
http://joseanpg.net/jslab/quiz/soshnikov/answers.html
其次是正美给的两个试题页面,这个之前倒是看过,而且难度较以上10题较为简单,具体可以参看司徒正美同学的页面
http://www.cnblogs.com/rubylouvre/archive/2010/01/28/1658434.html
http://www.cnblogs.com/rubylouvre/archive/2010/02/13/1667565.html