JavaScript optimized--06 mode (object)

Source: Internet
Author: User

Namespace mode:

  • Advantages: Can solve the naming confusion and third-party conflict;
  • Cons: Long nesting results in longer query times; more characters;
  • Universal namespace functions:
    var MyApp = MyApp | | {}; Myapp.namespace = function (ns_string) {    var parts = ns_string.split ('. ');    parent = MYAPP;      if (parts[0] = = = ' MYAPP ') {        parts = parts.slice (1);   }   for (var  i = 0; i < parts.length; i++) {      if (typeof parent[parts[i]] = = = ' undefined ') {          PARENT[PARTS[I]]
         = {};      }       Parent = Parent[parts[i]];   }   return parent; var module2 = myapp.namespace (' MYAPP.modules.module2 ');
  • Declaring dependencies: Although a modular naming has been made, the declaration of dependency can be more optimized
    For: MYAPP, MYAPP.util.Event, myapp.util.dom;//at the top of a function or module var  myFunc = function () {   var Event = MYAPP.util.Event;   

    The speed of parsing local variables is faster than resolving global variables;

  • Private properties and Privileged methods
    function Gadget () {   var name = ' IPod ';   This.getname = function () {        return name;   }}

    Note that if the private property setting is an object and is read-only, do not return it directly, but return its shallow copy;

JavaScript optimized--06 mode (object)

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.