Javascript object-oriented property implementation

Source: Internet
Author: User
Author: Truly
Date: 2007.8.3

In my previous articleArticleUse object-oriented in JavascriptWe introducedAn article by msdn 《Use object-oriented technology to create advanced Web ApplicationsProgram", The author briefly introduced some key techniques of JavaScript object-oriented, but the author made an obvious mistake when talking about the concept of closure:"Normally, local variables in the function cannot be accessed from outside the function. After the function exits, the local variable will disappear forever for various actual reasons.For more information, see the original document. In fact, this description is incorrect.

See the followingCode:

< Script >
Function Test (ABC)
{
This . G =   Function (){ Debugger ;};
}

VaRP= NewTest (2);
P. G ();
</Script>

If you have enabled the debugging function of IE and installed the script debugger, such as Vs, you can go to debugging when the program prompts debugging. At this time, you can see that ABC still exists, and keep the correct values intact, instead of permanently disappearing. However, this is not the focus of my article, but I hope you will be able to do more and practice in the future, such as MS ASP. the Software Design Engineers of the net Ajax team make such mistakes. What's more?

This article will discuss the common features of object-oriented programming.Attribute,However, attributes in Javascript cannot be as advancedProgramming LanguageIt can be used directly and looks more likeMethodThis implementation method is also calledClosureBut this article usesAttributeCommensurate.

AttributeIt is a protection method for private variables and provides the same effect as public variables. modern advanced programming languages such as C # and Java support the attribute feature.

We know that the function entry parameter is declared as the local variable of the function, and for the local variable, as described in "using object-oriented in JavaScript" about global variables and local variables, since its scope is limited to the inside of the function, all of them cannot be accessed externally, for example, P. ABC does not return the ABC variable in P. This is exactly the same as that in advanced programming languages. You cannot access its private variables outside the class, but we can use the public method to return private variables. Therefore, the role of attributes in advanced programming languages such as Java and C # is to protect private variables. For a language like C #, attributes will eventually be compiled by the compiler into methods such as get _ attribute name (). When we use an attribute, we actually call a method.

Use object-oriented technology to create advanced Web ApplicationsThe author believes that local variables survive because of the definition of the method. This is incorrect. We have analyzed the specific problem. If you still have doubts, then, study the following code carefully:

Function Person (name, age ){
This . Getname =   Function (){ Debugger ; Return   1 ;};
}
VaR O =   New Person ( 1 , 2 );
O. getname (); // After debugging, we found that name = 1
VaR T =   New Person ( 2 , 4 );
T. getname (); // After debugging, we found that name = 2
O. getname (); // After debugging, the system finds that name = 1 is not affected by other instances.

At first, I thought that the variables were not destroyed because they were referenced. Finally, it proved that the local variables were not destroyed before the object was destroyed.

At the same time, the other section in the article is inaccurate:
Note that these private members are slightly different from the private members we expect to generate from C. In C #, the public method of the class can access its private member. However, in Javascript, you can only access private members by having these private members in their closures (because these methods are different from common public methods, they are usually called privileged methods ). Therefore, in the public method of person, you must still use the privileged accesser method of the Private member to access the private member.

His statement is rather vague. In fact, we can understand it as follows:

In C #, we can set the private member variable of the category class in any method of the class,
In JavaScriptFunctionMethods defined inPrototypeMethod.

If you still cannot understand it in this way, you can still understand it in this way. Private variables in Javascript cannot be accessed outside of their declared functions, for example:

Function Person ()
{
VaR TTT;
}

Never in{}External Access AttemptTtt.

Now we have a better understanding of the features of variable scopes in JavaScript.

We have discussed the advantages of attributes in advanced programming languages and the protection of private variables by JavaScript. Therefore, you should be very clear about the implementation of attributes in JavaScript 《Use object-oriented technology to create advanced Web ApplicationsExample code in this article:

Function Person (name, age ){
This . Getname =   Function (){ Return Name ;};
This . Setname =   Function (Newname) {name = Newname ;};
This . Getage =   Function (){ Return Age ;};
This . Setage =   Function (Newage) {age = Newage ;};
}

The application and advantages of attributes in practice will be explained in my next article about custom events.

Postscript: I was going to describe how to implement some object-oriented features in Javascript, such as the encapsulation of objects embodied in the modern programming concept of "attribute: instead of directly operating the data content, you can access the data through the accessors, that is, you can use get and set to read and write private Member values. It turned into a philosophical discussion about whether a white horse is a horse.

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.