Define class members using prototype objects

Source: Internet
Author: User

Define class members using prototype objects
The previous section describes the implementation mechanism of the class and the implementation of the constructor. Now we introduce another machine that adds members to the class.
Format: Prototype object. When a new function is created, the members of this object are automatically assigned to the created object. For example
For example:
<Script language = "JavaScript" type = "text/JavaScript">
<! --
// Define a class with only one property prop
Function class1 (){
This. Prop = 1;
}
// Use the prototype attribute of the function to define a new member for the class.
Class1.prototype. showprop = function (){
Alert (this. Prop );
}
// Create an instance of class1
VaR obj1 = new class1 ();
// Call the showprop method defined by the prototype object
Obj1.showprop ();
// -->
</SCRIPT>
Because prototype is a JavaScript Object, you can add, modify, or delete a prototype object.
And attributes. To add a member definition for a class.
After learning about the prototype object of the function, let's take a look at the new execution process.
(1) create a new object and point this pointer to it;
(2) assign all the members of the prototype object of the function to this new object;
(3) execute the function body to initialize the object;
(4) return the object created in (1.
Compared with the execution process of new introduced in the previous section, prototype is more often used to initialize objects,
This is also consistent with the prototype literal meaning, which is the prototype of the corresponding class instance. This initialization process occurs in the letter
Before the number body (constructor) is executed, you can call the attributes and methods defined in prototype within the function body. For example:
For example:
<Script language = "JavaScript" type = "text/JavaScript">
<! --
// Define a class with only one property prop
Function class1 (){
This. Prop = 1;
This. showprop ();
}
// Use the prototype attribute of the function to define a new member for the class.
Class1.prototype. showprop = function (){
Alert (this. Prop );
}
// Create an instance of class1
VaR obj1 = new class1 ();
// -->
</SCRIPT>
Compared with the previous code, the showprop method defined in prototype is called in class1.
A dialog box is displayed during object construction. The value of the prop attribute is 1.
It should be noted that the definition of the prototype object must be prior to the statement for creating the class instance, otherwise it will not work, for example:
<Script language = "JavaScript" type = "text/JavaScript">
<! --
// Define a class with only one property prop
Function class1 (){
This. Prop = 1;
This. showprop ();
}
// Create an instance of class1
VaR obj1 = new class1 ();
// Use the prototype attribute of the function to define a new member for the class after the statement for creating the instance. This attribute is only valid for the objects created later.
Class1.prototype. showprop = function (){
Alert (this. Prop );
}
// -->
</SCRIPT>
This code will produce a runtime error, indicating that the object does not have the showprop method because of the definition of this method.
After instantiating a class statement.
It can be seen that prototype objects are dedicated to design class members and are closely related to a class. In addition,
Prototype also has an important attribute: constructor, which indicates a reference to the constructor. For example:
Function class1 (){
Alert (1 );
}
Class1.prototype. Constructor (); // call the constructor of the class.
After this code is run, a dialog box is displayed, showing the text "1", which makes it easier to see a prototype.
Is closely related to the definition of a class. Actually: class1.prototype. constructor = class1.

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.