Use of javascript static objects and constructor and public/private question _ javascript skills

Source: Internet
Author: User
The differences between static objects and constructors are usually in JSON format, or varobjfunction () {} or function () {} object construction methods, sometimes it is considered equivalent, but they are different. First look:

The Code is as follows:


Var objJson = {
Op1: 'objjson option1 ',
Fn1: function (){
Alert (this. op1)
}
}


In this form of declaration, you can directly access internal attributes through objJson. op1 or objJson. fn1 (). This is no problem. But if so:

The Code is as follows:


Var objFn = function (){
This. op1 = 'objfn. op1 ';
This. op2 = function (){
Alert (this. op1)
};
}


If you directly access the Internal Attributes of objFn. op1 or objFn. op2 (), it will not work because it is not an object at this time.
So we need to instantiate it.

The Code is as follows:


Var inst = new objFn ();
Alert (inst. op1 );
Inst. op2 ();


In this way, you can get the value you want.
If you want to copy an object in JSON format, it is very simple, but there is a problem:

The Code is as follows:


Var newone = objJson;
Newone. op1 = 'changed ';
Alert ('objjson. op1 ');


You will find that the op1 value in the original object has also changed. However, if the second object declaration method is used, the modification is only within the instance and will not affect other instances.
Therefore, static objects such as JSON are suitable for use when writing some frequently-used libraries. They have their own namespaces, and no one will interfere with them, and they are easy to use.
"Public" and "private" attributes of Constructors
Let's modify the above constructor:

The Code is as follows:


Var objFn = function (){
Var pri1 = 'private variable ';
This. op1 = 'public variable ';
This. op2 = function (){
Alert (pri1 + ',' + this. op1 );
};
};
Var o = new objFn ();
Alert (typeof o. pri1 + ',' + typeof o. op1); // undefined, string
O. op2 (); // Private variable, public variable


Private variables are not allowed to be accessed outside the object. All typeof variables are undefined. Next let's take a look at access to private methods:

The Code is as follows:


Var objFn = function (){
Var pri1 = 'private variable ';
Var pri2 = function (){
This. op2 ();
};
This. op1 = 'public variable ';
This. op2 = function (){
Alert (pri1 + ',' + this. op1 );
};
This. acPri = function (){
Pri2.call (this );
};
};
Var o = new objFn ();
O. acPri (); // Private variable, public variable


Note that because of the closure feature of JavaScript, we need to use call to pass the context of the program when calling the private method pri2 through the public method acPri, however, this seems to be too dizzy. You can wrap it a little:

The Code is as follows:


Var objFn = function (){
Var my = this;
Var pri1 = 'private variable ';
Var pri2 = function (){
My. op2 ();
};
This. op1 = 'public variable ';
This. op2 = function (){
Alert (pri1 + ',' + this. op1 );
};
This. acPri = function (){
Pri2 .();
};
};
Var o = new objFn ();
O. acPri (); // Private variable, public variable


Of course, the final result remains unchanged.
Reprinted please keep the following information
Author: Beiyu (tw: @ rehawk)

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.