I came across this article explaining Javascript quiz-legend. Anyway, it’s okay, so I want to bring it here for everyone to learn, understand, recite and criticize.
Question 1
(function(){ return typeof arguments;//"object"})();arguments is an Array-like object, corresponding to the parameter list of the passed function. You can use this variable directly in any function.
The typeof operator will only return results of type string. Refer to the following list to know what the value returned by typeof is the corresponding different data:
From this we infer that typeof arguments are object
Question 2
var f = function g(){ return 23; };typeof g();//Report an errorThis is a function expression with the name g, and then it is assigned to the variable f.
The function name g here and the variable f assigned by it have the following differences:
The function name g cannot be changed, while the variable f can be reassigned
The function name g can only be used inside the function body. If you try to use g outside the function, an error will be reported.
Question 3
(function(x){ delete x; return x;//1})(1);The delete operator can delete attributes from an object, and the correct usage is as follows:
delete object.property
delete object['property']
The delete operator can only be used on the object's properties and is invalid for variables and function names. That is to say, delete x is meaningless.
You'd better know that delete will not release memory directly, it just indirectly interrupts object reference
Question 4
var y = 1, x = y = typeof x; x;//"undefined"
We try to break down the above code into the following two steps:
var y = 1; //step 1
var x = y = typeof x; //step 2
There should be no objection to the first step, let's look at the second step directly
1. Assignment expressions are executed from right to left
2. The result of y being reassigned to typeof x, that is, undefined
3.x is assigned as the result of the right expression (y = typeof x), that is, undefined
Question 5
(function f(f){ return typeof f();//"number"})(function(){ return 1; });Direct comment explanation:
(function f(f){ //The f here is the parameter passed in function(){ return 1; } //The result of execution is naturally 1 return typeof f(); //So according to the table in question one, we know that typeof 1 result is "number"})(function(){ return 1; });Question 6
var foo = { bar: function() { return this.baz; }, baz: 1};(function(){ return typeof arguments[0]();//"undefined"})(foo.bar);Here you may mistakenly think that the end result is number. Passing parameters to a function can be regarded as an assignment, so arguments[0] gets the value of the real bar function, rather than the reference to foo.bar, so naturally this will not point to foo, but window.
Question 7
var foo = { bar: function(){ return this.baz; }, baz: 1}typeof (f = foo.bar)();//"undefined"This is the same problem as the previous question. (f = foo.bar) returns the value of bar, not its reference, so this does not refer to foo.
Question 8
var f = (function f(){ return '1'; }, function g(){ return 2; })();typeof f;//"number"The comma operator evaluates each of its operation objects (from left to right) and returns the value of the last operation object.
So the return value of (function f(){ return '1'; }, function g(){ return 2; }) is function g, and then execute it, then the result is 2; finally typeof 2, according to the table in question one, the result is naturally number
Question 9
var x = 1;if (function f(){}) { x += typeof f;}x;//"1undefined"The key point of this problem, we mentioned in question 2, the function name f in function expression cannot be accessed outside the function body.
Question 10
var x = [typeof x, typeof y][1];typeof typeof x;//"string" 1. Because the variable y has not been declared, typeof y returns "undefined"
2. Assign the result of typeof y to x, that is, x is now "undefined"
3. Then typeof x is of course "string"
4. The final result of typeof "string" is naturally still "string"
Question 11
(function(foo){ return typeof foo.bar;//"undefined"})({ foo: { bar: 1 } });This is a pure visual trick, commented
(function(foo){ //The foo here is { foo: { bar: 1 } }, and there is no bar attribute. //bar attribute is below foo.foo//So the result here is "undefined" return typeof foo.bar;})({ foo: { bar: 1 } });Question 12
(function f(){ function f(){ return 1; } return f();//2 function f(){ return 2; }})();Functions declared through function declaration can even be used before declaration, which we call this feature hoisting. So the above code is actually explained by the running environment like this:
(function f(){ function f(){ return 1; } function f(){ return 2; } return f();})();Question 13
function f(){ return f; }new f() instanceof f;//falseWhen the code new f() executes, the following will happen:
1. A new object is created. It inherits from f.prototype
2. The constructor f is executed. During execution, the corresponding pass will be passed in, and the context (this) will be specified as this new instance. new f is equivalent to new f() and can only be used in cases where no parameters are passed.
3. If the constructor returns an "object", then this object will replace the result of the entire new. If the constructor does not return the object, the result of new is the object created in step 1.
ps: Under normal circumstances, the constructor does not return any value, but if the user wants to overwrite this return value, he can choose to return a normal object to overwrite it. Of course, returning arrays will also be overwritten, because arrays are also objects.
Therefore, the new f() we here still returns the function f itself, not its instance
Question 14
with (function(x, undefined){}) length;//2
The with statement adds an object to the top of the scope chain. If there is a variable that has not used a namespace in the statement, which has the same name as a property in the scope chain, the variable will point to the property value. If there is a property with the same name, a ReferenceError exception will be issued.
OK, now let's look at it, since function(x, undefined){} is an anonymous function expression, which is a function, it will have a length attribute, which refers to the number of parameters of the function. So the end result is 2
Written at the end
Some people think these questions are tricky, while others think it broadens their horizons and depends on their own opinions. But one thing is true. Whether you are a firm practitioner or not, you will definitely not go far without theoretical foundation - you will never see any skilled technical worker suddenly becoming a Rocket expert.
Reading documents, reading standards, and combining practice is the decisive way for comrades.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.