JavaScript Perfection kill test and answer

Source: Internet
Author: User

The official address is http://perfectionkills.com/javascript-quiz/.
Copy codeThe Code is as follows:
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 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


The following is my answer:

Copy codeThe Code is as follows:
// Q1
(Function (){
Return typeof arguments; // it is clear that "object" should be returned here"
})();
// Q2
Var f = function g () {return 23 ;};
Typeof g (); // in ie, "number" is returned, but in ff, It is Error (g undefined)
// Q3
(Function (x ){
Delete x; // local variables cannot be deleted.
Return x; // so here 1 is returned.
}) (1 );
/*
Complimentary:
Delete has the following relationships with variables:
1. pre-defined properties of objects cannot be deleted; 2. attributes specified by prototype cannot be deleted.
// Regarding 2, can it be understood that only attributes dynamically attached to the object instance can be deleted?
3. variables defined by var (I personally think local variables) cannot be deleted. 4. variables and parameters defined by function (equivalent to local variables) cannot be deleted.
About delete, here are: 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 (); // here f refers to the passed anonymous function, so it should be "number" (that is, 1)
}) (Function () {return 1 ;});
// Q6
Var foo = {
Bar: function () {return this. baz ;},
// However, after the call, this points to bar, and bar does not have the 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 6th
Baz: 1
}
Typeof (f = foo. bar )();
// Q8
Var f = (function f () {return "1" ;}, function g () {return 2 ;})();
// Js, number operator, is called with 2nd functions, so "number" is returned"
Typeof f;
// Q9
Var x = 1;
If (function f (){}){
X + = typeof f; // 1 function and 1 undefined in ff in ie
}
X;
// Q10
Var x = [typeof x, typeof y] [1]; // x = "undefined"
Typeof x; // x is a string, so it should be "string"
// Q11
(Function (foo ){
Return typeof foo. bar;
// A very hidden question. The parameter foo points to the passed json object, and the json object has a foo attribute.
// Therefore, if it is typeof foo. foo. bar, it is "number"
// However, the foo parameter 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 overwrites 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 of which are functions. Therefore, the two are = and therefore false.
// If function f () {return 1;}, the returned object is
// Q14
With (function (x, undefined) {}) length;
// You can understand with (function (x, undefined) {}) {length ;}
// While length indicates the number of function parameters, it is 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.