JavaScript error-prone knowledge

Source: Internet
Author: User

1. In the Try-catch statement, the finally clause is optional, and when the finally clause is included, the return statement in either the try or catch statement is ignored. So, when we use the finally clause, we need to be clear about how we want the code to execute.

function fn () {     try  {         a++     ;     } Catch (Error) {         Console.log ("in Catch");          return ;     }      finally {         Console.log ("in finally");}     }  fn ();    // In catch in   finally

2. Void in Jvascript is an operator that specifies to evaluate an expression and return undefined. For example, the underscore uses void 0 instead of undefined.

3, undefined is not a reserved word (reserved word), it is only a property of the global object, in the low version of IE can be rewritten. So the direct use of undefined may not be reliable. So how do you ensure that the value of undefined is guaranteed to be taken? The following two scenarios can be used:

jquery version:

(function(window, undefined) {    //...}) (window);

The advantages of this writing:

    • Both window and undefined are designed to reduce the scope scope of variable lookups. When the window is passed to the inside of the closure, it can be used as a local variable when using it inside the closure, which is obviously faster than the original search under Window scope. (The original window is at the top of the scope chain, and the lookup speed is slow)
    • In jquery compressed version jquery.min.js, you can replace the local variable window with a single letter, reducing the file size and increasing the load speed.
    • Pass undefined as a parameter to the closure, because the value is not passed to it, and its value is undefined. So you can get the exact undefined.

Version underscore:

function (obj) {    returnvoid 0;}

4.

JavaScript error-prone knowledge

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.