From: JavaEye.com
注意JavaScript中对象类别的定义,使用function来定义对象类别,初始化对象使用new操作符
function Person(name, age) {
this.name = name;
this.age = age;
this.toString = function() {
document.writeln("[name]:"+this.name+"
"+"[age]:"+this.age);
}
}
完整的简单HTML
JavaScript <script> <BR>function Person(name, age) { <BR> this.name = name; <BR> this.age = age; <BR> this.toString = function() { <BR> document.writeln("[name]:"+this.name+"<br>"+"[age]:"+this.age); <BR> } <BR>} <BR></script>
<script> <BR> var person = new Person(); <BR> person.name="robbin"; <BR> person.age=30; <BR> person.toString(); <BR></script>