00-2. Javascript Specifications and considerations

Source: Internet
Author: User
Tags hasownproperty

  1. Variable declaration and scope
    1. Avoid declaring global variables
        1. A global variable is either declared outside of any function or is not declared straightforward to use.
        2. Reason: Portability. If you want your code to run under a different environment (under a host), use global variables on a thin ice, because you inadvertently overwrite a host object that does not exist in your initial environment (so you thought the name could be used with confidence, but in some cases it doesn't apply).
    2. JavaScript manages scopes through functions .
    3. Because of the two characteristics of JavaScript, it is easy to create global variables unconsciously
        1. First, you can use a variable without even needing a declaration, and JavaScript has an implicit global concept, meaning that any variable you don't declare will become a global object property
        2. Another scenario for creating an implicit global variable is to use a task chain for a partial var declaration.
          1. var a = b = 0; Right-to-left assignment, first, is the assignment expression B = 0, in which case B is not declared. Just like you entered: var a = (b = 0);
    4. The small difference between implicit global variables and well-defined global variables is the ability to make variables undefined by using the delete operator.
        1. Global variables created by VAR (created in programs other than functions) cannot be deleted.
        2. Implicit global variables created without VAR (regardless of whether they were created in a function) can be deleted.
          1. This indicates that, technically, implicit global variables are not real global variables, but they are properties of global objects. A property can be deleted by using the delete operator, but the variable is not:
    5. single Var , you can declare multiple variables using a var statement, separated by commas
        1. Benefits: provides a single place to find all the local variables needed for functionality
          It is good practice to initialize values like this initialization variable at the same time. This can prevent logic errors (all uninitialized but declared variables have an initial value of undefined) and increase the readability of the code. After you see the code, you can know the approximate purpose of the variables based on the initialized values, for example, as an object or as an integer.
        2. With the single var form, you can put the variable out of the loop, just like this:
          This form has the benefit of consistency because you stick to the single var form. The problem is that when refactoring the code, it's a little difficult to copy and paste the entire loop.
        3. Pre-parsing:The problem of Var dispersion
          1. In JavaScript, you can declare multiple var statements anywhere in a function, and they act as if they were declared at the top of a function, which is called hoisting (mount/sticky parsing/pre-parsing).
  2. Avoid implicit type conversions
      1. Always use the = = = and!== operators when you compare values and expression types.
  3. Naming conventions
    1. Because the constructor is essentially a function, the canonical function name can help tell you whether this should be a constructor or a normal function.
        1. When naming constructors, uppercase letters are suggestive, and functions and methods named with the first letter lowercase should not be called with NEW:
    2. Multi-word naming
        1. When your variable or function name has multiple words, the separation of the best words follows a uniform norm,
          1. For Constructors , you can use the large hump-type nomenclature , such as: Myconstructor ()
          2. For functions and methods , you can use small hump-like nomenclature , such as: MyFunction (), CalculateArea (), and Getfirstname ()
          3. What if it's a variable ?
            1. Developers usually use small hump-style nomenclature , but there is another way to do this: all words are underlined in lowercase. For example, first_name, Favorite_bands, and old_company_name, this notation helps you visually differentiate between functions and other identities (prototypes and objects).
    3. Other naming forms
        1. There is no way to define constants in JavaScript, so developers use the full capitalization specification to name variables that do not change in the program's life cycle.
        2. There is another formula for full capitalization: The global variable name is all capitalized .
        3. Another way to simulate functionality using a specification is private members, and developers find it easier to use only an underscore prefix to represent a private property or method. The following are some common _private specifications:
          1. Use a trailing underline to represent private , such as Name_ and Getelements_ ()
          2. Use an underscore prefix to represent the _protected (protection) attribute , and the two underscore prefixes represent __private (private) attributes
          3. Some of the built-in variable properties in Firefox are not part of the technical portion of the language, using two front underscores and two post -underline representations, such as: __proto__ and __parent__
  4. Try not to eval (), easy to contaminate the global
      1. It is also important to remember that passing strings to the SetInterval (), SetTimeout (), and function () constructors, in most cases, is similar to using eval () and is therefore avoided. Behind the scenes, JavaScript still needs to parse and execute the strings that you pass to the program:
      2. If you absolutely must use eval (),
        1. You might consider using the new Function () (similar to eval ()) instead . And there is a small potential benefit because the code evaluation in the new function () is run in the scope of the local function , so any variables that are evaluated in the code that are defined by VAR will not automatically become global variables.
          1. The difference from eval ()
            1. eval () can access and modify variables in its outer scope , which is not function ( Note: using function and new function are the same ).
        2. Another way to prevent an automatic global variable is to encapsulate the eval () call into an immediate function . Because eval () uses a variable object in the execution context in which it is invoked.
      3. expansion, creation and reference of dynamic variables ! Finishing
  5. For-in Cycle
      1. The array uses a normal for loop, and the object uses the for-in loop.
        1. The hasOwnProperty () method allows you to filter out attributes from the prototype chain when traversing object properties.
          1. Another form of using hasOwnProperty () is to cancel the object.prototype on the way. Like this:
            The benefit: Avoid naming conflicts that redefine hasownproperty with the man object . Of course, in order to avoid all the methods of long property lookup objects, you can "cache" it using local variables.

  6. Curly braces {}
      1. Curly braces (also weigh brackets, hereinafter) should always be used, even when they are optional. Technically, in if judgment, in or for if the statement is only one, curly braces are not needed, but you should always use them, which makes the code more persistent and easy to update.
      2. Position of the left curly brace
        1. The developer has a different preference for the position of the opening brace-in the same row or on the next line. But in the example below, the brackets will behave differently depending on the position of the parentheses-this is because the semicolon insertion mechanism , when you choose to end a line of code without a semicolon, will fix it for you.
          This behavior can cause trouble, such as when you return an object literal, while the opening parenthesis is on the next line:
          You will be surprised if you want the function to return an object that contains the name attribute. Because of the implicit semicolon, the function returns undefined. The preceding code is equivalent to:
  7. Sss

00-2. Javascript Specifications and considerations

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.