From: JavaEye.com
Pay attention to the definition of object classes in JavaScript. Use function to define object classes. initialize objects using the new operator.
Function Person (name, age ){
This. name = name;
This. age = age;
This. toString = function (){
Document. writeln ("[name]:" + this. name + "<br>" + "[age]:" + this. age );
}
}
Complete simple HTML
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> JavaScript </title>
<Script language = "javascript" type = "text/javascript">
Function Person (name, age ){
This. name = name;
This. age = age;
This. toString = function (){
Document. writeln ("[name]:" + this. name + "<br>" + "[age]:" + this. age );
}
}
</Script>
</Head>
<Body>
<Script type = "text/javascript">
Var person = new Person ();
Person. name = "robbin ";
Person. age = 30;
Person. toString ();
</Script>
</Body>
</Html>