下面是选择题:官方地址是 http://perfectionkills.com/javascript-quiz/
1.
(function(){
return typeof arguments;
})();
“object”
“array”
“arguments”
“undefined”
2.
var f = function g(){ return 23; };
typeof g();
“number”
“undefined”
“function”
Error
3.
(function(x){
delete x;
return x;
})(1);
1
null
undefined
Error
4.
var y = 1, x = y = typeof x;
x;
1
“number”
undefined
“undefined”
5.
(function f(f){
return typeof f();
})(function(){ return 1; });
“number”
“undefined”
“function”
Error
6.
var foo = {
bar: function() { return this.baz; },
baz: 1
};
(function(){
return typeof arguments[0]();
})(foo.bar);
“undefined”
“object”
“number”
“function”
7.
var foo = {
bar: function(){ return this.baz; },
baz: 1
}
typeof (f = foo.bar)();
“undefined”
“object”
“number”
“function”
8.
var f = (function f(){ return "1"; }, function g(){ return 2; })();
typeof f;
“string”
“number”
“function”
“undefined”
9.
var x = 1;
if (function f(){}) {
x = typeof f;
}
x;
1
“1function”
“1undefined”
NaN
10.
var x = [typeof x, typeof y][1];
typeof typeof x;
“number”
“string”
“undefined”
“object”
11.
(function(foo){
return typeof foo.bar;
})({ foo: { bar: 1 } });
“undefined”
“object”
“number”
Error
12.
(function f(){
function f(){ return 1; }
return f();
function f(){ return 2; }
})();
Error (e.g. “Too much recursion”)
undefined
13.
function f(){ return f; }
new f() instanceof f;
true
false
14.
with (function(x, undefined){}) length;
undefined
Error
下面是个人的解答:
// Q1
(function(){
return typeof arguments; //Obviously what should be returned here is "object"
})();
// Q2
var f = function g(){ return 23; };
typeof g(); //Under ie, what is returned here is "number", but under ff, it is Error (g is not defined )
// Q3
(function(x){
delete x; //It should be said that local variables cannot be deleted
return x; //So here returns 1
}) (1);
/*
Gift:
delete and variables have the following relationship:
1. The predefined attributes of the object cannot be deleted; 2. The attributes specified through prototype cannot be deleted delete's
// Regarding 2, it can be understood that only attributes dynamically attached to object instances can be deleted?
3. Variables defined by var (which I personally understand as local variables) cannot be deleted; 4. Variables and parameters defined by function (equivalent to local variables) cannot be deleted.
For an introduction to delete, here is: http://tech.idv2.com/2008/01/09/javascript-variables-and-delete-operator/
*/
// Q4
var y = 1, x = y = typeof x;
x; //Obviously, x should be undefined
// Q5
(function f(f){
return typeof f( ); //The f here refers to the anonymous function passed in, so it should be "number" (that is, 1)
})(function(){ return 1; });
// Q6
var foo = {
bar: function() { return this.baz; },
//However, after the call, this here points to bar, and bar has no baz attribute, so it is undefined
baz: 1
};
(function(){
return typeof arguments[0](); //arguments[0] is foo.bar
})(foo.bar );
// Q7
var foo = {
bar: function(){ return this.baz; }, //Same as question 6
baz: 1
}
typeof (f = foo.bar)();
// Q8
var f = (function f(){ return "1"; }, function g(){ return 2; })();
//The sign operator of js is called the second function, so the returned value is "number"
typeof f;
// Q9
var x = 1;
if (function f(){}) {
x = typeof f; //ie is 1function, ff is 1undefined
}
x;
// Q10
var x = [typeof x, typeof y][1]; //x="undefined"
typeof typeof x; //x is a string, so it should be "string"
// Q11
(function( foo){
return typeof foo.bar;
//A very obscure question, the parameter foo points to the passed in json object, and the json object has a foo attribute.
//Therefore, if it is typeof foo.foo.bar here, it is "number"
//However, the parameter foo itself does not have the bar attribute, so it is "undefined"
})({ foo: { bar: 1 } });
// Q12
(function f(){
function f(){ return 1; }
return f(); // Function definition, The latter overrides the former, so it is 2
function f(){ return 2; }
})();
// Q13
function f(){ return f; }
new f() instanceof f;
//new f() is actually the same thing as f, both are functions, therefore, both are ==, so they are false
//If function f(){ return 1;}, then what is returned is object
// Q14
with (function(x, undefined){}) length;
//It will be clear when you write it like this {}){length;}
//length refers to the number of formal parameters of the function, so it is 2