JS var self = this;

Source: Internet
Author: User

Var self = this // refers to the object in this scope

We provide a private self parameter. This allows the object to be visible to private methods. This is because private methods cannot be called by public methods. To make private methods useful
They are appended to objects, but they cannot be accessed from outside, and they cannot be accessed by the public methods of this object (prototype methods are all public. They are only visible to private members. The private method is the internal function of the constructor. An error in ECMAScript Language Specification. this error causes this to fail to set the internal function correctly.

 

Code
Function Class1 (){
// Self (self is appended to the object) self is only visible to private members (Can. Click I aa (). Click pubpubpublic_dd ())
Var self = this;
This. I = 1;
This. aa = function (){
This. I ++;
Alert (this. I );
}
Var private_bb = function (){
Alert (self. I );
// Self. public_dd (); // error self cannot be accessed from outside, and self cannot be accessed by the public methods of this object
// Aa (); // The error private method must be called through self
Public_dd (); // self. public_dd ();
Self. aa ();
}
This. cc = function (){
Private_bb (); // Private function
}

// Can be called directly
// Public method of the object
Function public_dd ()
{
Self. aa ();
Alert ("dd ");
}
}
Var o = new Class1 (); // call the Class1 constructor without running ++ (initialization without calling or running)
O. cc (); // run ++
Document. write (o. I); // return 2

 

 

Variables declared using var have local scopes, which means they can only be accessed within the functions they define. If var is not used, the scope of the variable is global, that is, variables can be accessed by all JavaScript code anywhere on the Web page (or any external JS library contained in this page.
If you have global variables and local variables with the same name, it is important to set the scope of the variables. Develop good JavaScript programming habits from the beginning. One of these habits is to explicitly define the scope of a variable.
The following are some rules for scope considerations:
If the variable in the function uses the keyword var during declaration, it can only be used locally in this function.
If the variable in the function is declared without the keyword var, and a global variable with the same name exists, it will be treated as the global variable.
If the variable declares and uses the keyword var in the local part, but it is not initialized (for example, the value is assigned), it can be accessed but not defined.
If the variable is declared locally, but the keyword var is not used, or it is explicitly declared as a global variable, but it is not initialized, it can be accessed globally but not defined.
By using var in a function, you can prevent problems caused by using global variables with the same name and local variables. This is especially important when using external JavaScript libraries.

 

 

 

GetCatalogTree: function (CId, strId ){
...
},
Functions: function (catalogId ){
This. getCatalogTree (0, "tree"); // call other methods outside of this
This. option. onSuccess = function (transport ){
If (self. ChkMng (transport. responseText ))
{
Self. getCatalogTree (0, "tree"); // call other methods in self
}
};
}

 

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.