JavaScript使用prototype定义对象类型(转)[
From: www.eastmoney.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();
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如下:
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();
郑重声明:用户在财富号/股吧/博客社区发表的所有信息(包括但不限于文字、视频、音频、数据及图表)仅仅代表个人观点,与本网站立场无关,不对您构成任何投资建议,据此操作风险自担。请勿相信代客理财、免费荐股和炒股培训等宣传内容,远离非法证券活动。请勿添加发言用户的手机号码、公众号、微博、微信及QQ等信息,谨防上当受骗!
郑重声明:用户在社区发表的所有资料、言论等仅仅代表个人观点,与本网站立场无关,不对您构成任何投资建议。用户应基于自己的独立判断,自行决定证券投资并承担相应风险。《东方财富社区管理规定》