From: JavaEye.com prototype provides a JavaScript object-oriented infrastructure, which can be used for object-oriented programming. The method for defining object types is as follows:
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 Person constructor. The complete HTML is as follows:
Copy codeThe Code is as follows: <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Test Object </title>
<Script type = "text/javascript" src = "prototype. js"> </script>
</Head>
<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>
</Html>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.