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); } }
先使用Class.create()来创建一个对象类型,然后定义该对象类型,注意initialize方法是Person的构造器,完整的HTML如下:
代码如下:
html
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
titleTest Object/title
script type="text/javascript" src="prototype.js"/script
/head
body
script type="text/javascript"/DIV
DIV class=codevar 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=codevar person = new Person("robbin",30);
person.toString();
/script
/body
/html
猜你喜欢