14 Tortured JavaScript-side questions

Source: Internet
Author: User

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"}) ();   

argumentsis a Array-like object that corresponds to the argument list of the passed-in function. You can use the variable directly in any function.

typeofThe 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);     

deleteAn operator can delete an attribute from an object, using the correct usage as follows:

Delete object.propertyobject[' property '] 

deleteAn 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

    1. Assignment expression right-to-left execution

    2. yThe result of being re-assigned typeof x , i.e.undefined

    3. xis 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" 
    1. Because no variables have been declared y , the typeof y return"undefined"

    2. The typeof y result x is assigned, which means that x it is now"undefined"

    3. Then typeof x of course it is."string"

    4. 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 declarationthe 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:

    1. A new object is created. It inherits fromf.prototype

    2. 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 fequal to new f() , can only be used in cases where no parameters are passed.

    3. 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  

withStatement 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

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.