< script type = "Text/javascript" >
function person () {
Property
This.head = 1;
This.eye = 2;
Method
This.eat = function () {
Alert ("Eat something");
}
}
Methods for extending classes
Person.prototype.run =function () {
Alert ("I'll Run");
}
function Programmer () {
this.coding = function () {
Alert ("I'll Knock the Code");
}
}
Methods for extending classes
Programmer.prototype.run =function () {
Alert ("I'll Go");
}
Inherited
Programmer.prototype = new Person ();
Add a new method to a subclass
Programmer.prototype.debug = function () {
Alert ("I'll Debug the Code");
}
Invoke Example
function docoding () {
var a = new Programmer ();
alert (A.head); Calling the properties of the parent class
A.eat (); Calling a method of the parent class
A.debug (); Methods for calling Subclasses
}
Docoding ();
var p = new Programmer ();
P.run ();
Execute Person.run () why? Mainly see p.__proto__ point is the object of the __proto__.
p.__proto__
person {head:1, eye:2}
From here we can see that the p.__proto__ point is the person object, all will execute Person.run ()
P.__proto__.run ();
I can run.
</script>
Summarize:
1. Prototype is a property of a class (function object). Function: Extend the properties and methods of a class, to inherit other objects (properties and methods)
2. __proto__ is a property of the object inside (var obj = function person () {}). Function: Prototype that points to the corresponding class of the object
JavaScript prototype and __proto__