JS中prototype關(guān)鍵字的功能介紹及使用示例
更新時(shí)間:2013年07月21日 18:06:21 作者:
prototype 關(guān)鍵字可以為JS原有對(duì)象或者自己創(chuàng)建的類中添加方法或者屬性。也可以實(shí)現(xiàn)繼承,下面以實(shí)例的方式為大家詳細(xì)介紹下
prototype 關(guān)鍵字可以為 JS原有對(duì)象 或者 自己創(chuàng)建的類 中添加方法或者屬性。
也可以實(shí)現(xiàn)繼承。
例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS中 prototype 關(guān)鍵字的使用</title>
</head>
<script>
<!-- demo1 JS中原有對(duì)象中 添加方法 -->
Number.prototype.add = function (num){
return this+num;
}
function but1_click(){
alert((3).add(8));
}
<!-- demo2 JS中新建對(duì)象中, 添加屬性 ,方法 -->
function Car(cColor,cWeight){
this.cColor = cColor;
this.cWeight = cWeight;
}
Car.prototype.drivers = new Array('zhangsan','lisi');
Car.prototype.work = function (cLong){
alert("我跑了"+cLong+"公里");
}
function but2_click(){
var c = new Car("red","5");
c.drivers.push('zhaoliu');
alert(c.drivers);
c.work(1);
}
<!-- demo3 JS中新建對(duì)象中, 添加屬性 ,方法 緊湊的寫(xiě)法 -->
function Rectangle(rWeight,rHeight){
this.rWeight = rWeight;
this.rHeight = rHeight;
if( typeof this._init == 'undefined'){
Rectangle.prototype.test = function (){
alert("test");
}
}
this._init = true;
}
function but3_click(){
var t = new Rectangle(6,8);
t.test();
}
<!-- demo4 prototype 繼承 -->
function objectA(){
this.methodA = function (){
alert("我是A方法");
}
}
function objectB(){
this.methodB = function (){
alert("我是B方法");
}
}
objectB.prototype = new objectA();
function but4_click(){
var t = new objectB();
t.methodB();
t.methodA();
}
</script>
<body>
<h2> prototype 關(guān)鍵字的使用 </h2>
<hr />
<input id="but1" type="button" value="demo1" onclick="but1_click()" />
<input id="but2" type="button" value="demo2" onclick="but2_click()" />
<input id="but3" type="button" value="demo3" onclick="but3_click()" />
<input id="but4" type="button" value="demo4" onclick="but4_click()" />
</body>
</html>
也可以實(shí)現(xiàn)繼承。
例子:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS中 prototype 關(guān)鍵字的使用</title>
</head>
<script>
<!-- demo1 JS中原有對(duì)象中 添加方法 -->
Number.prototype.add = function (num){
return this+num;
}
function but1_click(){
alert((3).add(8));
}
<!-- demo2 JS中新建對(duì)象中, 添加屬性 ,方法 -->
function Car(cColor,cWeight){
this.cColor = cColor;
this.cWeight = cWeight;
}
Car.prototype.drivers = new Array('zhangsan','lisi');
Car.prototype.work = function (cLong){
alert("我跑了"+cLong+"公里");
}
function but2_click(){
var c = new Car("red","5");
c.drivers.push('zhaoliu');
alert(c.drivers);
c.work(1);
}
<!-- demo3 JS中新建對(duì)象中, 添加屬性 ,方法 緊湊的寫(xiě)法 -->
function Rectangle(rWeight,rHeight){
this.rWeight = rWeight;
this.rHeight = rHeight;
if( typeof this._init == 'undefined'){
Rectangle.prototype.test = function (){
alert("test");
}
}
this._init = true;
}
function but3_click(){
var t = new Rectangle(6,8);
t.test();
}
<!-- demo4 prototype 繼承 -->
function objectA(){
this.methodA = function (){
alert("我是A方法");
}
}
function objectB(){
this.methodB = function (){
alert("我是B方法");
}
}
objectB.prototype = new objectA();
function but4_click(){
var t = new objectB();
t.methodB();
t.methodA();
}
</script>
<body>
<h2> prototype 關(guān)鍵字的使用 </h2>
<hr />
<input id="but1" type="button" value="demo1" onclick="but1_click()" />
<input id="but2" type="button" value="demo2" onclick="but2_click()" />
<input id="but3" type="button" value="demo3" onclick="but3_click()" />
<input id="but4" type="button" value="demo4" onclick="but4_click()" />
</body>
</html>
您可能感興趣的文章:
- JS 面向?qū)ο笾衿娴膒rototype
- javascript 進(jìn)階篇3 Ajax 、JSON、 Prototype介紹
- 深入分析js中的constructor和prototype
- 為JS擴(kuò)展Array.prototype.indexOf引發(fā)的問(wèn)題探討及解決
- JS中的prototype與面向?qū)ο蟮膶?shí)例講解
- 解析jQuery與其它js(Prototype)庫(kù)兼容共存
- 判斷js中各種數(shù)據(jù)的類型方法之typeof與0bject.prototype.toString講解
- js中繼承的幾種用法總結(jié)(apply,call,prototype)
- js的Prototype屬性解釋及常用方法
- js類定義函數(shù)時(shí)用prototype與不用的區(qū)別示例介紹
- js使用Array.prototype.sort()對(duì)數(shù)組對(duì)象排序的方法
- 深入淺析JavaScript中prototype和proto的關(guān)系
相關(guān)文章
javascript中驗(yàn)證大寫(xiě)字母、數(shù)字和中文
本文為大家介紹下使用javascript驗(yàn)證大寫(xiě)字母小寫(xiě)字母,數(shù)字和中文,具體示例如下2014-01-01js位運(yùn)算在實(shí)際中使用的實(shí)例教程
我們可能很少在編程中用位運(yùn)算,如果沒(méi)深入學(xué)習(xí),可能也很難理解,下面這篇文章主要給大家介紹了關(guān)于js位運(yùn)算在實(shí)際中使用的相關(guān)資料,需要的朋友可以參考下2022-03-03在Swiper內(nèi)如何制作CSS3動(dòng)畫(huà)效果示例代碼
這篇文章主要給大家介紹了關(guān)于在Swiper內(nèi)如何制作CSS3動(dòng)畫(huà)效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12javascript代碼在ie8里報(bào)錯(cuò) document.getElementById(...) 為空或不是對(duì)象的解決方
今天更升級(jí)了ie8,發(fā)現(xiàn)原來(lái)在ie7下可以運(yùn)行的代碼,不能運(yùn)行了,發(fā)現(xiàn)了一些細(xì)節(jié),附臨時(shí)修改辦法。2009-11-11JavaScript算法題之如何將一個(gè)數(shù)組旋轉(zhuǎn)k步
這篇文章主要給大家介紹了關(guān)于JavaScript算法題之如何將一個(gè)數(shù)組旋轉(zhuǎn)k步的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03腳本吧 - 幻宇工作室用到j(luò)s,超強(qiáng)推薦base.js
腳本吧 - 幻宇工作室用到j(luò)s,超強(qiáng)推薦base.js...2006-12-12在網(wǎng)頁(yè)里看flash的trace數(shù)據(jù)的js類
我的js類jdhcn.js中的一個(gè)flashDebug方法2009-01-01