From:JavaEye.com prototype provides a JavaScript object-oriented infrastructure that we can use for object-oriented programming, defining object types in the following ways:
var person = class.create (); Person.prototype = {initialize:function (name, age) {this.name = name; This.age = age; }, Tostring:function () {Document.writeln ("[Name]:" +this.name+ "<br>" + "[age]:" +this.age); } }
First use Class.create () to create an object type, and then define the object type, note that the Initialize method is the constructor for person, and the complete HTML is as follows:
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>test object</title>
<script type= "Text/javascript" src= "Prototype.js" ></script>
<body>
<script type= "Text/javascript" ></DIV>
<div Class=code>var person = class.create ();
Person.prototype = {
Initialize:function (name, age) {
THIS.name = name;
This.age = age;
},
Tostring:function () {
Document.writeln ("[Name]:" +this.name+ "<br>" [age]: "+this.age);
}
}</div>
<div Class=code>var person = new Person ("Robbin", 30);
Person.tostring ();
</script>
</body>