Your location:Tech News>News>Script>14 grueling JavaScript face questions

Latest News

14 grueling JavaScript face questions



  Turn to the interpretation of this article quiz-legend Javascript quiz, anyway, all right, wanted to move over for them to learn, understand, recite, criticism.

  Question one

  (Function () {

  return typeof arguments; // "object"

  }) ();

  Array-like arguments is a target, is the corresponding argument list of a function.You can use this variable directly in any function.

  typeof operator returns only the result of type string.It understood with reference to the following Table correspond to different data, what values are returned typeof:


  From this we infer, typeof arguments are object

  Question two

  var f = function g () {return 23;};

  typeof g (); // given

  This is a name g the function expression, and then was assigned to the variable f.

  Where g and the function name assigned by its variable f has the following differences:

  G can not change the function name, and the variable f can be reassigned

  G function name can only be used inside the function body, trying to use the g complains as an external function

  Question three

  (Function (x) {

  delete x;

  return x; // 1


  delete operator can remove the attributes from the object, the correct use of the following:


  delete object [ ''property'']

  delete operator can act on the properties of the object, and the function of the variable name is invalid.That makes no sense to delete x.

  You''d better know, delete is not directly release the memory of her only indirectly interrupt object reference

  Question four

  var y = 1, x = y = typeof x; x; // "undefined"

  We tried to break down the above code into the following two steps:

  var y = 1; // step 1

  var x = y = typeof x; // step 2

  The first step should be no objection, we look directly at the second step

  1.Assignment expression from right to left execution

  2.y be reassigned as a result of typeof x, which is undefined

  3.x is assigned the expression on the right (y = typeof x) the result is undefined

  Five questions

  (Function f (f) {

  return typeof f (); // "number"

  }) (Function () {return 1;});

  Comment directly on to explain:

  (Function f (f) {

  // f here is passed parameter function () {return 1;}

  // execution is the result of natural 1

  return typeof f (); // it is a problem according to the form we know, typeof 1 is the result of "number"

  }) (Function () {return 1;});

  Question six

  var foo = {

  bar: function () {return this.baz;},

  baz: 1


  (Function () {

  return typeof arguments [0] (); // "undefined"

  }) (;

  Here you may mistakenly think the end result is the number.The transfer function may be seen as a parameter assignment, the arguments [0] is the value obtained is a function of the real bar, not foo.This reference bar, then naturally this will not point to foo, but the window.

  Question seven

  var foo = {

  bar: function () {return this.baz;},

  baz: 1


  typeof (f = (); // "undefined"

  This last question is the same question, (f = return value is the bar, rather than its reference, then this will not refer to the foo.

  Question eight

  var f = (function f () {return ''1'';}, function g () {return 2;}) ();

  typeof f; // "number"

  Each operation object evaluate the comma operator and returns the value of its last operation target (from left to right),

  So (function f () {return ''1'';}, function g () {return 2;}) return value is the function g, and then carry out her, then the result is 2; Finally typeof 2, according to issue a table the result is a natural number

  Nine on

  var x = 1;

  if (function f () {}) {

  x + = typeof f;


  x; // "1undefined"

  The key point of this issue, we talked about in question two, function expression in the name of the function f is not accessible outside the function body

  Question 10

  var x = [typeof x, typeof y] [1];

  typeof typeof x; // "string" 1.Because there is no declared variable y, so typeof y returns "undefined"

  2.The result typeof y is assigned to x, that is to say x is now "undefined"

  3.Then of course typeof x is "string"

  4.Finally typeof "string" is the result of natural or "string"

  Question 11

  (Function (foo) {

  return typeof; // "undefined"

  }) ({Foo: {bar: 1}});

  This is a purely visual trick, comment on

  (Function (foo) {

  Here // foo, is {foo: {bar: 1}}, and there is no bar properties oh.

  // bar property is below

  // So here is the result of "undefined"

  return typeof;

  }) ({Foo: {bar: 1}});

  Problem 12

  (Function f () {

  function f () {return 1;}

  return f (); // 2

  function f () {return 2;}

  }) ();

  You can even use the function before the function declaration statement by statement, a feature we call hoisting.So in fact, the above code is to be construed operating environment:

  (Function f () {

  function f () {return 1;}

  function f () {return 2;}

  return f ();

  }) ();

  Thirteen issues

  function f () {return f;}

  new f () instanceof f; // false

  When the code is new f () executed, the following will happen:

  1.A new object is created.It inherited from f.prototype

  2.The constructor is executed f.When executed, the corresponding transmission participants is passed, while the context (this) will be assigned a new instance of f is equivalent to the new f (), it can only be used without any parameters passed.

  3.If the constructor returns an "object", then the object will replace the results of the entire new out.If the constructor does not return the object, then the new results come out as an object created in step 1,

  ps: general constructor function does not return under any value, but if users want to override the return value, you can choose to return to a normal object covered.Of course, the return will cover an array, because arrays are objects.

  So, here''s new f () returns still function f itself, rather than his example

  Problem fourteen

  with (function (x, undefined) {}) length; // 2

  with statement at the top of the scope chain to add an object, if there is an unused variables in the namespace statement in a property with the same name as the scope chain, then this variable will point to the value of this property.If there is no property of the same name, ReferenceError will throw an exception.

  OK, now we look, since the function (x, undefined) {} is a function expression anonymous, is a function, there will be a length property, it refers to the number of arguments.So the end result is 2

  Written in the last

  Some people feel that these issues pit father, some people think that broadened my horizons, a matter of opinion.But one thing is true, whether you send a firm practice, the lack of a theoretical foundation, it would definitely not far away - you''ll never see what skilled workers suddenly became a rocket scientist.

  Look at the document, read standard, with practice, is the road to victory comrades.

  That''s all for this article, I want to be helpful to learn, I hope you will support script Home.

  You may also be interested in the article: Detailed JS in this, apply, call, bind (classic interview questions) common interview questions about the scope of the share JS javascript interview questions 10 typical interview questions 80% of JavaScript candidates are not qualified Javascript front-end classic interview questions and answers interview questions thrown on a Thoughts JavaScript javascript type conversion of the three most common interview questions parse JavaScript JS Sohu surface analysis questions interview questions (pointer, hats and girlfriend) interview on the prototype js JavaScript face questions Daquan explain the problem (recommended) AngularJS face questions highlights js finishing the front end face questions and answers (a) JS face questions --- questions about stairs of an elegant algorithm analysis js face questions in fn () and return fn () is face questions about the difference between a JavaScript variable scope of an often despised web front-end common interview questions (JS) latest Javascript programmer interview questions and problem-solving methods

Recommend article

Relate article