JavaScript使用prototype定義對(duì)象類型
更新時(shí)間:2007年02月07日 00:00:00 作者:
From: JavaEye.com prototype提供了一套JavaScript面向?qū)ο蠡A(chǔ)設(shè)施,我們可以使用它來進(jìn)行面向?qū)ο缶幊?,定義對(duì)象類型方式如下:
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()來創(chuàng)建一個(gè)對(duì)象類型,然后定義該對(duì)象類型,注意initialize方法是Person的構(gòu)造器,完整的HTML如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Object</title>
<script type="text/javascript" src="prototype.js"></script>
</head>
<body>
<script type="text/javascript"></DIV>
<DIV class=code>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);
}
}</DIV>
<DIV class=code>var person = new Person("robbin",30);
person.toString();
</script>
</body>
</html>
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()來創(chuàng)建一個(gè)對(duì)象類型,然后定義該對(duì)象類型,注意initialize方法是Person的構(gòu)造器,完整的HTML如下:
復(fù)制代碼 代碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Object</title>
<script type="text/javascript" src="prototype.js"></script>
</head>
<body>
<script type="text/javascript"></DIV>
<DIV class=code>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);
}
}</DIV>
<DIV class=code>var person = new Person("robbin",30);
person.toString();
</script>
</body>
</html>
您可能感興趣的文章:
- 判斷js中各種數(shù)據(jù)的類型方法之typeof與0bject.prototype.toString講解
- js類定義函數(shù)時(shí)用prototype與不用的區(qū)別示例介紹
- JavaScript使用prototype定義對(duì)象類型(轉(zhuǎn))[
- JavaScript類和繼承 prototype屬性
- JavaScript子類用Object.getPrototypeOf去調(diào)用父類方法解析
- javascript基于prototype實(shí)現(xiàn)類似OOP繼承的方法
- JS偽繼承prototype實(shí)現(xiàn)方法示例
- 淺談js構(gòu)造函數(shù)的方法與原型prototype
- js實(shí)現(xiàn)prototype擴(kuò)展的方法(字符串,日期,數(shù)組擴(kuò)展)
- Javascript中prototype屬性實(shí)現(xiàn)給內(nèi)置對(duì)象添加新的方法
- JS利用prototype給類添加方法操作詳解
相關(guān)文章
prototype 1.5 & scriptaculous 1.6.1 學(xué)習(xí)筆記
prototype 1.5 & scriptaculous 1.6.1 學(xué)習(xí)筆記...2006-09-09Prototype Selector對(duì)象學(xué)習(xí)
這個(gè)對(duì)象在幫助文檔上并沒有,但是這個(gè)對(duì)象確是整個(gè)DOM操作的核心類,工具函數(shù)$$,其實(shí)就是調(diào)用這個(gè)類的方法2009-07-07prototype Element學(xué)習(xí)筆記(篇一)
Element,哈哈哈。遇到正主了,到現(xiàn)在為止才遇到讓我高興的玩意。當(dāng)初Ext.Element可是花三千余行代碼專門來封裝啊。我倒要看一看它的代碼了。事實(shí)上prototype中我最想研究的只有兩個(gè)內(nèi)容:Element、Selector。這兩個(gè)東西是精華。2008-10-10Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($w,$F方法)
Prototype $w $F使用方法2009-07-07Prototype Function對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象就是對(duì)function的一些擴(kuò)充,最重要的當(dāng)屬bind方法,其中wrap方法也很重要,在類繼承機(jī)制里面就是利用wrap方法來調(diào)用父類的同名方法。2009-07-07Prototype String對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象里面的方法就是提供了一些字符串操作的工具方法,比較重要的gsub方法,下面做了詳細(xì)的注釋,簡(jiǎn)單的方法就不說了,一看就明白了。2009-07-07Prototype的Class.create函數(shù)解析
Prototype中的類的創(chuàng)建,一般使用Class.create方法來創(chuàng)建,例如PeriodicalExecuter類型。使用的時(shí)候通過調(diào)用new PeriodicalExecuter(xxx)來生成對(duì)象。2011-09-09滾動(dòng)經(jīng)典最新話題[prototype框架]下編寫
滾動(dòng)經(jīng)典最新話題[prototype框架]下編寫...2006-10-10