JavaScript使用prototype定義對(duì)象類(lèi)型
更新時(shí)間:2007年02月07日 00:00:00 作者:
From: JavaEye.com prototype提供了一套JavaScript面向?qū)ο蠡A(chǔ)設(shè)施,我們可以使用它來(lái)進(jìn)行面向?qū)ο缶幊?,定義對(duì)象類(lèi)型方式如下:
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()來(lái)創(chuàng)建一個(gè)對(duì)象類(lèi)型,然后定義該對(duì)象類(lèi)型,注意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()來(lái)創(chuàng)建一個(gè)對(duì)象類(lèi)型,然后定義該對(duì)象類(lèi)型,注意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ù)的類(lèi)型方法之typeof與0bject.prototype.toString講解
- js類(lèi)定義函數(shù)時(shí)用prototype與不用的區(qū)別示例介紹
- JavaScript使用prototype定義對(duì)象類(lèi)型(轉(zhuǎn))[
- JavaScript類(lèi)和繼承 prototype屬性
- JavaScript子類(lèi)用Object.getPrototypeOf去調(diào)用父類(lèi)方法解析
- javascript基于prototype實(shí)現(xiàn)類(lèi)似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給類(lèi)添加方法操作詳解
相關(guān)文章
Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($方法)
$方法——被成為瑞士軍刀$方法使用技巧。2009-07-07
使用prototype.js 的時(shí)候應(yīng)該特別注意的幾個(gè)問(wèn)題.
使用prototype.js 的時(shí)候應(yīng)該特別注意的幾個(gè)問(wèn)題....2007-04-04
Prototype Object對(duì)象 學(xué)習(xí)
該不是一個(gè)概念。因?yàn)镃#中的命名空間后面不會(huì)直接跟方法,肯定是接一個(gè)對(duì)象然后在調(diào)用方法,不過(guò)和C++中的命名空間倒是有些類(lèi)似2009-07-07
JavaScript使用prototype定義對(duì)象類(lèi)型
JavaScript使用prototype定義對(duì)象類(lèi)型...2007-02-02
prototype 1.5 & scriptaculous 1.6.1 學(xué)習(xí)筆記
prototype 1.5 & scriptaculous 1.6.1 學(xué)習(xí)筆記...2006-09-09
由prototype_1.3.1進(jìn)入javascript殿堂-類(lèi)的初探
由prototype_1.3.1進(jìn)入javascript殿堂-類(lèi)的初探...2006-11-11
類(lèi)之Prototype.js學(xué)習(xí)
類(lèi)之Prototype.js學(xué)習(xí)...2007-06-06
Prototype 工具函數(shù) 學(xué)習(xí)
Prototype學(xué)習(xí)工具函數(shù)$H,$R,Try.these,document.getElementsByClassName2009-07-07

