Interesting JS quiz: test your core JS internal strength

Source: Internet
Author: User

I recently read a technical blog from a foreign company and saw a blog post on "javascript small test", which is very interesting. Each of the topics is short and concise, however, the knowledge of core javascript can be well explored. if you are interested, clickJavascript quizView the original article. To make a record, I plan to list these questions below and give explanations. If there is any explanation that is not in place, you are welcome to point it out.

There are several notes:

    • The following questions adopt the ecma3 standard (not 5)
    • Implementation of the quirks mode is not considered
    • EachCodeThe running context of the fragment is global.
    • No other variables except the variables declared in the Code are declared.
    • The answer depends on the value returned by the expression/statement (or the last line ).
  1. (Function () {return typeof arguments ;})();

    "Object", there seems to be no explanation, under the firebug log.

  2.  var F = function g () {return 23 ;}; typeof g (); 

    runtime error, note that the first line "=" is a function Declaration on the right, for example, function funcname (){...}, "=" Var F declares the variable F on the left, and the entire statement declares and initializes F. In this form, funcname can only be used as a reference of the function in the function body, external access is unavailable, so g is undefined, g () error. Here we can extend it to arguments in ecma5. callee has been deprecated, so we can replace it with the above features, as shown in the following code:

     (function funcname (){//... some tasks here // then, after 1 s do more // here we use funcname to replace arguments. callee which now is deprecated in ecma5settimeout (funcname, 1000)}) (); 
  3. (Function (x) {Delete X; return x ;}) (1 );

    1. X is only a variable in a function scope. Delete X has no practical significance here, and many interesting questions can be raised about delete operations, here is a blog post detailing the delete operation. ClickUnderstanding DeleteView Original

  4. Var y = 1, x = y = typeof X;

    "Undefined" is mainly used to execute the value assignment statement from right to left. First, y = typeof X. If X is not defined yet, Y is "undefined", and then x = Y, X is initialized to "undefined"

  5. (Function () f (f) {return typeof F () ;}) (function () {return 1 ;});

    "Number". If the function name is the same as the parameter name, the "Priority" of the parameter name is higher than that of the function name with the same name.

  6. VaR Foo = {bar: function () {return this. baz ;}, Baz: 1 }; (function () {return typeof arguments [0] () ;}) (FOO. bar );

    "Undefined", we can see Foo. bar is passed as a parameter, foo. bar is a function. In fact, arguments [0] is a reference to this function. When this function is executed through arguments [0] (), its running context is not Foo, so this. baz is "undefined". After further analysis, you will find that at runtime, this points to arguments in return typeof arguments [0] ();

  7. VaR Foo = {bar: function () {return this. Baz ;}, Baz: 1} typeof (F = Foo. bar )();

    "Undefined", as explained in the previous question, F is a reference to the foo. Bar function, which points to window

  8. VaR F = (function f () {return "1" ;}, function g () {return 2 ;}) (); typeof F;

    "Number", which is mainly a comma (,) expression. Remember the value of the comma (,) expression, which is determined by the last expression. in this way, the problem is very simple. The comma (,) expression Returns function g, and the execution function g returns 2, which is "number.

  9. VaR x = 1; if (function f () {}) {x + = typeof F;} X;

    "1 undefined" is mainly about function f () {}. Here, this function declaration is used as a conditional expression of if, instead of being declared under global context. function Name F, which cannot be accessed outside the function, so typeof F is "undefined"

  10. VaR x = [typeof X, typeof y] [1]; typeof X;

    "String": var x = ["undefined", "undefined"] [1]

  11. (Function (FOO) {return typeof Foo. Bar;}) ({FOO: {bar: 1 }});

    Runtime error. It is clear that {FOO: {bar: 1} is passed in, so it should be Foo. Foo. Bar so that the code can pass normally.

  12. (Function f () {return 1;} return F (); function f () {return 2 ;}})();

    2. Remember: when processing code, the JavaScript engine puts the declaration of variables and functions in the scope header in advance. Therefore, the above Code is equivalent to the following code:

    (Function f () {return 1;} function f () {return 2;} // override previous function declarationreturn F ();})();
  13. Function f () {return F;} new F () instanceof F;

    False. Although the new operator is used, function f returns its own reference, so the new here does not play a role.

  14.  with (function (x, undefined) {}) length; 

    2. With expressions are rarely used, or are not recommended at all, with is used to bind the context of its block-level code. Here length can be seen as this. length, and this points to function (x, undefined) {}, obviously there are two parameters

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.