Using this and prototype, You can implement Object-Oriented Programming of JavaScript and build the concepts of classes and instances. First, the class is the abstraction of the object, and the instance is the actual object. The object can be instantiated with the keyword "new.
This is used to access the instance from the program code of the instance. The prototype object is used to set the features and methods of the class level, rather than those of the instance. The method can be stored inside the class to prevent the instance from repeatedly copying program code.
The following is a simple implementation:
<Html>
<Head>
<Script>
Function Person (id, name ){
This. id = id;
This. name = name;
}
Person. prototype. showme = function (){
Return "id =" + this. id + "; name =" + this. name;
}
Function test (){
Var p = new Person (1, "jim ");
Var p2 = new Person (2, "kate ");
Alert (p. showme () + "\ n" + p2.showme ());
}
</Script>
</Head>
<Body>
<Input type = "button" value = "test" onclick = "test ()"/>
</Body>
</Html>
This article is from the "jian Jian" blog