Ecmascript 2 (object usage)

Source: Internet
Author: User
Tags variable scope

Ecmascript 2 (object usage)

1. Scope of objects and variables

A. Compared with other object-oriented languages, strictly speaking, ecmascript does not have a static scope. The sub-methods in object methods are the public scope methods of methods, rather than static methods.

B. This keyword always points to the object that calls the method. It is best to use this keyword when calling the attributes of this object. Otherwise, the compiler will regard the attributes as local or global variables for reference.

C. Promotion of variable scope

        var name = "jay";        function show() {            alert(name);            var name = "boy";            alert(name);        }        show();

View code

The ecmascript specification describes that all JavaScript code runs in one execution context. execution context is not an accessible entity in Javascript, but it is very important to fully understand the working principles of functions and closures. the specification says:

When the control is transferred to the executable code of ecmascript, the control enters an execution context. The execution context logic of the activity forms a stack. The execution context at the top of the stack is the currently running execution context.

Each execution context has a variable object. All variables and functions defined in the function are added as attributes of this object. The following algorithm describes this process.

For any form parameter, add the corresponding attributes on the variable object and set their values to the values passed to the function.

For any function declaration, add the corresponding attribute to the variable object. The value is the function. if a function declaration uses the same identifier as any of the parameters, this attribute (the parameter) will be overwritten.

For any variable declaration, Add corresponding attributes to the variable object and initialize their values as undefined, regardless of how these variables are initialized in the source code. if a variable uses the same identifier as a defined attribute, the defined attributes will not be overwritten.

This algorithm is used to improve the declaration of functions and variables. Note that, although functions are promoted as a whole, only their declaration is promoted. Please note that the initialization of the variable occurs where the variable is defined in the source code.

 

2. How to construct private public methods and attributes of objects.

Generally, we use the Iife module mode to organize code into modules, write class libraries, and abstract public code.

How to define the namespace to abstract the corresponding module code? For details, refer to jquery source code:

  

 (function( window, undefined ) {     var jQuery = function( selector, context ) {   // The jQuery object is actually just the init constructor 'enhanced'        return new jQuery.fn.init( selector, context, rootjQuery );     },      // Expose jQuery to the global object     window.jQuery = window.$ = jQuery;  })( window ); 

// The module mode mainly serves to differentiate private variables/functions and public variables/functions.

(Function ($, undefined) {// defines the mycommon object var mycommon ={}; // Private Attribute VaR _ color, _ name, _ createtime; // public attribute mycommon. color = function () {return this. color;} // Private method VaR _ showcolor = function () {alert ('color');} $. mycommon = mycommon;}) ($ );

Through the above method, we can well implement encapsulation and module-based extension.

 

 

Reference: http://developer.51cto.com/art/201304/389494.htm, NLP advanced programming.

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.