Each function has a prototype attribute, called a prototype. Each object also has a prototype, which can be accessed through __proto__ in the Firefox/safari/chrome/opera, and no related interfaces are provided in IE6/7/8.
Copy Code code as follows:
function person () {
This.method1 = function () {}
}
PERSON.PROTOTYPE.METHOD2 = function () {}
Function Man () {}
Man.prototype = new Person ();
MAN.PROTOTYPE.M1 = function () {}
Man.prototype.m2 = function () {}
var m = new Man ();
For (Var A in m.__proto__) {
alert (a);
}
Defines the parent class person, the subclass man. New Man object, print out all attributes.
The ECMAScript V5 adds a static Getprototypeof method (Firefox/chrome implemented) to object to obtain the prototype of the objects. It can imitate the super of Java.
Copy Code code as follows:
function person () {
This.method1 = function () {alert (1)}
}
PERSON.PROTOTYPE.METHOD2 = function () {alert (2);}
Function Man () {
THIS.M1 = function () {
Object.getprototypeof (This). METHOD1 ();
}
}
Man.prototype = new Person ();//prototype inheritance
Man.prototype.m2 = function () {
Object.getprototypeof (This). METHOD2 ();
}
var mans = new Man ();
MAN.M1 ();
MAN.M2 ();
The M1 method hanging on this in the subclass man calls the method1 on this in the parent class person, and the M2 method on the prototype calls prototype on the parent class METHOD2.
As you can see, the object prototype includes not only the properties on its constructor prototype, but also the properties on this in the constructor. Of course, because of the context in JavaScript, this in the parent class cannot be automatically converted in subclasses and requires some skill to do so.
This is the case in Java
Copy Code code as follows:
Package bao1;
Class Person {
private String name;
Person (String name) {
THIS.name = name;
}
public void Method1 () {
System.out.println (this.name);
}
}
Class Man extends person{
Man (String name) {
Super (name);
}
public void M1 () {
Super.method1 ();
}
}
public class Test {
public static void Main (string[] args) {
Mans man1 = new Man ("Jack");
MAN1.M1 ();
}
}