JavaScript object-oriented (1)

Source: Internet
Author: User

I. Traditional Prototype-based approach
To be exact, the "class" of distinct Cr into pt cannot be regarded as a real class in a strict sense. There is a relationship between the declared object and the class (estimated:
Instance. _ proto __= InstanceClass. prototype (not supported by ie)
The preceding description: The instance property (_ proto _) is equal to the prototpye of the class. We can see that, the "_ proto _" of the Instance happens to be connected to "prototype" of the class. The prototype is used to find and expand its Method. We will find its Method) and its properties are both Public (you can also use Private, that is, do not use this), prototype is actually an object, used to record methods as a set
Suppose we declare A Class.
Function (){
This. className = "ClassA"; // Add the Public attribute. The class name should be Private.
}
// Add a method
A. prototype. getClassName = function (){
Return this. className; // this indicates A, not prototype.
}
// Declare an object
Var AObj = new ();
Alert (AObj. _ proto __= = A. prototype); // invalid in ie, true in ff, which indicates that the above verification is correct
// Let's take a look at object attributes.
/*
Because this is used, the className is Public and can be modified or read at will.
If you want to protect it, you need to use Private, you can remove its this prefix as protection.
When reading, use a public method, that is, the method under prototype:
A. prototype. getClassName = function (){
Return className; // does not work, does not exist, className is undefined
}
Here, we can only agree to add this to the className and do not directly access it. At this point, it is not as good as the second method.
Description later
*/
About "prototype"
Because Javascr extends pt has not used "inheritance" in the system (it is very likely), all prototype obtained under the class is
The top-level Object can be inherited later. We can see the following formula:
Child. prototype = new parent ();
Inheritance with quotation marks means that even inheritance is not really an inheritance, it is only obtained through prototype.
To extend a method to the class using an instance, we have to mention the _ proto _ attribute. Copy codeThe Code is as follows: <scr Using pt LANGUAGE = "using Cr Using pt">
<! --
Function abc (){
Var writable wner = this;
Owner. k = "pp ";
Owner. abc = function (){
}
}
Abc. prototype. def = function (){
}
Var s = new abc ();
For (var t in abc. prototype ){
Alert ("prototype points to:" + t + "=" + abc. prototype [t]);
}
For (var t in s. _ proto __){
Alert ("_ proto _ pointing to:" + t + "=" + s. _ proto _ [t]);
}
// -->
</Scr platinum pt>

In ff, I can see the Instance name. _ proto __= class. prototype. Now, we use the instance extension method. This method is not recommended in actual applications, as shown in the following example:
S. _ proto _. hjk = function (){
Return "hjk ";
}
Alert (s. hjk ());
For (var t in abc. prototype ){
Alert ("prototype points to:" + t + "=" + abc. prototype [t]);
}
This should be clear.

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.