Front-end engineers sometimes encounter a class of interviewers during an interview, and the questions they ask are very seriously to the language itself, often not a practical question that the candidate might expect (some candidates stress the ability to work, as well as knowing that the reason is irrelevant). This kind of topic, although not logical, but to some extent, did examine the candidate's javascript
understanding of the language.
Suddenly think of this topic is bored on their own github, to see what has been written before the ugly goods. Then turned to this explanation of JavaScript quiz article quiz-legend, anyway, I want to move over for everyone to learn, understand, recite, criticize.
Question one
(function () { arguments; "Object"}) ();
arguments
is a Array-like object that corresponds to the argument list of the passed-in function. You can use the variable directly in any function.
typeof
The operator returns only string
the result of the type. Refer to the following list to see typeof
what values are returned for different data:
type |
Results |
undefined |
‘undefined‘ |
null |
‘object‘ |
Boolean |
‘boolean‘ |
Number |
‘number‘ |
String |
‘string‘ |
Symbol (New in ECMAScript 2015) |
‘symbol‘ |
Host object (provided by the JS environment) |
Implementation-dependent |
Function object (implements [[Call]] in ECMA-262 terms) |
‘function‘ |
Any other object |
‘object‘ |
From this we infer that typeof arguments
it isobject
question two
G (23;}; typeof G (); //Error
This is a g
function expression with the name, which is then assigned to the variable f
.
There are differences between the function name g
and the variable assigned to it f
:
Function names g
cannot be changed, and variables f
can be re-assigned
Function names g
can only be used in the body of the functions, attempting to use outside of the function g
will be an error
question three
(function (x) { delete x; return x; //1}) (1);
delete
An operator can delete an attribute from an object, using the correct usage as follows:
Delete object.propertyobject[' property ']
delete
An operator can only function on an object's properties, not on a variable or function name. In other words, delete x
there is no point.
You'd better know that delete
it's not going to release the memory directly, she just interrupts the object reference indirectly.
question four
var y = 1, x = y = typeof x; x;//"undefined"
We are trying to break down the above code into two steps:
Step 1//step 2
The first step should be no objection, we look directly at the second step
Assignment expression right-to-left execution
y
The result of being re-assigned typeof x
, i.e.undefined
x
is assigned the y = typeof x
result of the right expression (), which isundefined
question five
(F(f) { typeof F (); "Number"}) (function (1;});
Directly on the annotation explanation:
(F(f) { //F Here is the passed-in parameter function () {return 1;} //So according to the form of question one we know, typeof 1 result is "number"}) (function (1;});
question six
var foo = { function (1};(function (arguments[0] (); "Undefined"}) (Foo.bar);
Here you may mistakenly assume that the end result is number
. Passing a parameter to a function can be thought of as an assignment, so getting the value of a arguments[0]
real bar
function is not foo.bar
the reference, then nature this
does not point foo
to it window
.
question Seven
var foo = { function (1}typeof (F = foo.bar) (); "Undefined"
This is the same problem as the previous one, and the (f = foo.bar)
return is the bar
value, not its reference, then it is this
not foo
.
question eight
var f = (F (g (2;}) (); typeof F; //"number"
The comma operator evaluates each of its operands (from left to right) and then returns the value of the last Action object
So (function f(){ return ‘1‘; }, function g(){ return 2; })
the return value is the function g
, then executes her, then the result is 2
; finally typeof 2
, according to the table of question one, the result is naturallynumber
question Nine
1; if (f (typeof F;} X //"1undefined"
The key point of this problem, as we've talked about in question two, is that the function expression
function name f
is not accessible outside the function body.
question 10
var x = [typeof x, typeof y][1];typeof typeof x;//"string"
Because no variables have been declared y
, the typeof y
return"undefined"
The typeof y
result x
is assigned, which means that x
it is now"undefined"
Then typeof x
of course it is."string"
typeof "string"
The final result is naturally"string"
question 11
(function (foo) { typeof Foo.bar; 1}});
It's a purely visual trick, on the note.
(function (foo) { //foo here, is {foo: {bar:1}}, and there is no bar attribute Oh. The//bar attribute is under Foo.foo 1});
Question 12
(F() { Freturn F (); F2;}}) ();
function declaration
the declared function can even be used before the declaration, which we call hoisting. So the above code is actually explained by the running environment:
(F() { fFreturn F ();}) ();
Question 13
Freturn F;} instanceof F; //false
When new f()
the code executes, the following things will happen:
A new object is created. It inherits fromf.prototype
The constructor f
is executed. At the time of execution, the corresponding delegate is passed in, and the context ( this
) is specified as the new instance. new f
equal to new f()
, can only be used in cases where no parameters are passed.
If the constructor returns an "object", the object replaces the entire new
result. If the constructor does not return an object, then new
the result is the object created in step 1,
PS: In general, constructors do not return any values, but if the user wants to override the return value, they can choose to return a normal object to overwrite. Of course, the returned array is also overwritten because the array is also an object.
So, what we're new f()
returning here is still the function f
itself, not his instance.
question 14
With (function (x, undefined) {}) length; 2
with
Statement adds an object to the top of the scope chain, if there is a variable in the statement
namespace that is not used, and the same name as a property in the scope chain, this variable will point to this property value. If there is no property with the same name, the exception is thrown out ReferenceError
.
OK, now let's see, because function(x, undefined){}
it is an anonymous function expression, is a function, there will be length
attributes, refers to the function of the number of arguments. So the end result is 2
.
written in the last
Some people think that these problems pit dad, also some people think that broaden their horizons, a matter of opinion. But one thing is true, whether you are a firm practitioner, a lack of a theoretical foundation, and a certainty that you will never see a skilled worker suddenly become a rocket expert.
Looking at documents, reading standards, combining practice, is the way to win the comrades.
14 Tortured JavaScript-side questions