Reprinted from: http://blog.csdn.net/chaojie2009/article/details/6719353 (also reproduced. Despise this person reprint not famous source. )
Note: You must look at this article with suspicion, some of which describe some micro-problems. By the way to see the comments.
PS: You can think of prototype as a member variable, thinking about the order of the search members caused by the characteristics of JS syntax.
JavaScript is Object-based, and any element can be considered an object. However, the types and objects are different. In this article, in addition to discussing some of the characteristics of types and objects, it is more important to study how to write good and reusable types. After all, JavaScript, a popular scripting language, is very meaningful for reuse if it can be well encapsulated and form a large type library. There are many articles on the Internet for prototype, and I have never understood the core idea. Finally, after writing a lot of example code to understand: prototype can only be used on the type. Here are some examples of types and objects that might make it easier to understand the connection between a type and an object after reading an example:
|
Example code |
Description |
1 |
Object.prototype.Property = 1; Object.prototype.Method = function () {alert (1),} var obj = new Object (), alert (obj. property), obj. Method (); |
You can use Proptotype on a type to add behavior to a type. These behaviors can only be represented on instances of the type.
The allowable types in JS are array, Boolean, Date, Enumerator, Error, Function, number, Object, REGEXP, String |
2 |
var obj = new Object (); Obj.prototype.Property = 1; Error//errorobj.prototype.method = function () {alert (1);} |
Prototype cannot be used on an instance, or a compilation error occurs |
3 |
Object.property = 1;object. Method = function () {alert (1);} alert (object.property); Object. Method (); |
You can define a "static" property and method for a type, which is called directly on the type. |
4 |
Object.property = 1;object. Method = function () {alert (1);} var obj = new Object (), alert (obj. property); Errorobj.method (); Error |
An instance cannot invoke a static property or method of a type, or an object undefined error occurs. |
5 |
function AClass () {this. property = 1;this. Method = function () {alert (1);}} var obj = new AClass (); alert (obj. property), obj. Method (); |
This example demonstrates the usual way to define a type in JavaScript |
6 |
function AClass () {this. property = 1;this. Method = function () {alert (1);}} Aclass.prototype.Property2 = 2; ACLASS.PROTOTYPE.METHOD2 = function{alert (2);} var obj = new AClass (); alert (obj. Property2); obj. METHOD2 (); |
You can add properties and methods to custom types externally using prototype. |
7 |
function AClass () {this. property = 1;this. Method = function () {alert (1);}} Aclass.prototype.Property = 2; Aclass.prototype.Method = function{alert (2);} var obj = new AClass (); alert (obj. property), obj. Method (); |
The property or method of the custom type cannot be changed externally by prototype. As you can see, the properties and methods of the call are still the result of the original definition. |
8 |
function AClass () {this. property = 1;this. Method = function () {alert (1);}} var obj = new AClass (); obj. property = 2;obj. Method = function () {alert (2);} Alert (obj. property), obj. Method (); |
Properties can be changed on the object. (This is yes) you can also change the method on the object. (Different from the universal object-oriented concept) |
9 |
function AClass () {this. property = 1;this. Method = function () {alert (1);}} var obj = new AClass (); obj. Property2 = 2;obj. METHOD2 = function () {alert (2);} Alert (obj. Property2); obj. METHOD2 (); |
You can add properties or methods on an object |
function aclass () { this. property = 1; . Method = function () { alert (1); }} function AClass2 () { this. Property2 = 2; this. METHOD2 = function () { alert (2); }}aclass2.prototype = new AClass (); var obj = new AClass2 (); alert (obj. property), obj. Method (); alert (obj. Property2); obj. METHOD2 (); |
|
function aclass () { this. property = 1; . Method = function () { alert (1); }} function AClass2 () { this. Property2 = 2; this. METHOD2 = function () { alert (2); }}aclass2.prototype = new AClass (); AClass2.prototype.Property = 3; AClass2.prototype.Method = function () { alert (4);} var obj = new AClass2 (); alert (obj. property), obj. Method (); |
This example illustrates how subclasses can override the properties or methods of a parent class. |
In the above example, it is important to implement reuse by type: • The type of behavior allowed in the example 1:javascript • Example 2:prototype limitations used • Example 3: How to define a static member on a type · Example 7:prototype limitations on members of a redefined type • Example 10: How to make one type inherit from another type • Example 11: How to redefine the members of a parent class in a subclass visible the object-oriented features that JavaScript can implement are: • Public properties field) • Public method • Private attribute (private field) • Proprietary Method • Method overload • Constructor (constructor) • Event (. Event) • Single inherit • Subclass overrides the parent class's property or method (override) • Static property or method (static member)
(turn) JS prototype detailed