发表于 2019-04-26 18:00:34 股吧网页版
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();


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





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();


","post_publish_time":"2014-01-28 17:29:39","post_last_time":"2014-01-28 17:29:39","post_ip":"","post_state":0,"post_click_count":574,"post_forward_count":0,"post_comment_count":0,"post_like_count":0,"post_is_like":false,"post_is_edited":false,"post_is_collected":false,"post_type":7,"post_top_status":0,"post_status":0,"post_from":"股吧网页版","post_pdf_url":"","post_has_pic":false,"post_pic_url":[],"pre_postid":219393854,"pre_post_title":"Javascript中克隆一个数组的实现代码","post_postid":0,"post_post_title":"","post_comment_authority":0,"post_ip_address":"","repost_state":0,"reptile_state":0,"allow_likes_state":0,"system_comment_authority":0,"extend":"","rc":1,"me":"操作成功","error_code":0,"rUrl":"http://gbapi.em/blog/api/blog/BlogContent?postid=219334941&ctoken=&utoken=&deviceid=blog&product=guba&version=200&plat=Web&ip=216.73.217.119"}}