Book Notes: Writing maintainable JavaScript

Source: Internet
Author: User

  1. Use the for-in loop to filter using the hasOwnProperty method, unless you want to find the inheritance property on the prototype chain
  2. You should not use the for-in loop for array traversal, only the properties of object should be traversed
  3. The end of each line of expression to use a semicolon (;), in order to make the package work, you should also add a semicolon to the beginning of each JS file, such as such an anonymous function definition; (function () {...} ());
  4. Variable initialization should be assigned null because TypeOf is an operator that returns an object for an uninitialized variable, while an undeclared variable returns undefine
  5. The result of null==undefined is true, and the result of null===undefined is false, so we should use ===,!==
  6. Do not attach properties directly to object construction objects, but to initialize an object with JSON-formatted notation
  7. The declaration of all variables in the function body is advanced by the parser ahead of the first sentence, because the JS engine performs the function by first scanning all variables within the scope, regardless of where it is placed in the function body
  8. Although all declarations are advanced, and the function declaration takes precedence over the variable declaration with the same name, the function declaration cannot be overwritten as long as the variable is initialized at the same time as the declaration.
  9. A function declaration should not be placed in the middle or behind a block of code, especially in the judgment condition, and different browsers interpret it differently, but assigning an anonymous function to an expression does not apply to this rule
  10. Do not use eval, and do not pass in strings in functions such as Function,settimeout,setinterval
  11. Do not embed JavaScript expressions in the CSS, which will cause the browser to repeat the calculation, so that the overall performance of the system degradation, and this kind of JS into the CSS is not worth advocating
  12. Do not manipulate the object's CSS style in JavaScript. This is more error-prone than the previous one, as we often modify the style property in DOM elements
  13. The secret of separating CSS from JS and maintaining the connection between them is that JS is only responsible for manipulating CSS classname, and classname style definition in CSS.
  14. Instead of embedding JavaScript code snippets directly into HTML, put JavaScript code in a separate JS file and use <script> to refer to external files
  15. Do not embed HTML code directly in JavaScript, use the JavaScript template engine, and then render through variables a little better
  16. Handlebars is an excellent JavaScript template engine that can help you implement templates + variables = Dynamic HTML, and in front-end frameworks such as Ember.
  17. There are two ways to handle a JavaScript global variable conflict problem
    1. One is to use a single global variable
      1. That is, all internal variables of an application system are placed under a global variable, and this global variable is in the context of the JavaScript engine
      2. JavaScript's built-in objects, such as Array,date, you can think of them as a single global variable, because these variables are likely to be used in almost every application
      3. The application of single global variables involves the partitioning of namespaces and the loading of modules
      4. For module loading, you can use the AMD (Asynchronous module definition), which provides global functions define for loading dependent modules
      5. To use AMD, you need a compatible module loader, such as requirejs,http://www.requirejs.org/
      6. REQUIREJS provides a require global function to load a module and perform a method callback after a successful load
    2. The second is to use 0 global variables
      1. 0 The use of global variables, mainly through the immediate invocation of anonymous functions to achieve
      2. Although zero global variables do not cause global pollution, they are often used in a limited number of scenarios
      3. Typically used for page initial loading, or for a one-time execution of code, and it does not interact with other code, nor external output interface
  18. When handling events, the event object is always held only by the event-handling logic and the application logic is not directly accessed by the application logic.
  19. Use the typeof operator to detect the base type, detect reference types with instanceof, and detect null with = = =
  20. Detection function: typeof MyFunc = = = "function"
  21. Detection array: Array.isarray (MyArray)
  22. Detection properties: MyAttr in MyObject, Object.hasownproperty
  23. Since the JS parser does not protect any code including the execution environment, application developers should not modify any code other than their own, including the native type of JS, Dom,bom, and third-party class libraries such as jquery.
  24. Ant can also be used as a build tool for JavaScript


Book Notes: Writing maintainable JavaScript

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.