您好,欢迎来到居食网。
搜索
您的当前位置:首页JavaScript使用prototype定义对象类型(转)[_javascript技巧

JavaScript使用prototype定义对象类型(转)[_javascript技巧

来源:居食网


From: JavaEye.com

prototype提供了一套JavaScript面向对象基础设施,我们可以使用它来进行面向对象编程,定义对象类型方式如下: var Person = Class.create();
Person.prototype = {
initialize : function(name, age) {
this.name = name;
this.age = age;
},
toString : function() {
document.writeln("[name]:"+this.name+"
"+"[age]:"+this.age);
}
}
先使用Class.create()来创建一个对象类型,然后定义该对象类型,注意initialize方法是Person的构造器,完整的HTML如下:


Test Object



var Person = Class.create();
Person.prototype = {
initialize : function(name, age) {
this.name = name;
this.age = age;
},
toString : function() {
document.writeln("[name]:"+this.name+"
"+"[age]:"+this.age);
}
}

var person = new Person("robbin",30);
person.toString();


Copyright © 2019- jusc.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务