JS中prototype關鍵字的功能介紹及使用示例
更新時間:2013年07月21日 18:06:21 作者:
prototype 關鍵字可以為JS原有對象或者自己創(chuàng)建的類中添加方法或者屬性。也可以實現(xiàn)繼承,下面以實例的方式為大家詳細介紹下
prototype 關鍵字可以為 JS原有對象 或者 自己創(chuàng)建的類 中添加方法或者屬性。
也可以實現(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 關鍵字的使用</title>
</head>
<script>
<!-- demo1 JS中原有對象中 添加方法 -->
Number.prototype.add = function (num){
return this+num;
}
function but1_click(){
alert((3).add(8));
}
<!-- demo2 JS中新建對象中, 添加屬性 ,方法 -->
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中新建對象中, 添加屬性 ,方法 緊湊的寫法 -->
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 關鍵字的使用 </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>
也可以實現(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 關鍵字的使用</title>
</head>
<script>
<!-- demo1 JS中原有對象中 添加方法 -->
Number.prototype.add = function (num){
return this+num;
}
function but1_click(){
alert((3).add(8));
}
<!-- demo2 JS中新建對象中, 添加屬性 ,方法 -->
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中新建對象中, 添加屬性 ,方法 緊湊的寫法 -->
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 關鍵字的使用 </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 進階篇3 Ajax 、JSON、 Prototype介紹
- 深入分析js中的constructor和prototype
- 為JS擴展Array.prototype.indexOf引發(fā)的問題探討及解決
- JS中的prototype與面向?qū)ο蟮膶嵗v解
- 解析jQuery與其它js(Prototype)庫兼容共存
- 判斷js中各種數(shù)據(jù)的類型方法之typeof與0bject.prototype.toString講解
- js中繼承的幾種用法總結(jié)(apply,call,prototype)
- js的Prototype屬性解釋及常用方法
- js類定義函數(shù)時用prototype與不用的區(qū)別示例介紹
- js使用Array.prototype.sort()對數(shù)組對象排序的方法
- 深入淺析JavaScript中prototype和proto的關系
相關文章
javascript代碼在ie8里報錯 document.getElementById(...) 為空或不是對象的解決方
今天更升級了ie8,發(fā)現(xiàn)原來在ie7下可以運行的代碼,不能運行了,發(fā)現(xiàn)了一些細節(jié),附臨時修改辦法。2009-11-11JavaScript算法題之如何將一個數(shù)組旋轉(zhuǎn)k步
這篇文章主要給大家介紹了關于JavaScript算法題之如何將一個數(shù)組旋轉(zhuǎn)k步的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03在網(wǎng)頁里看flash的trace數(shù)據(jù)的js類
我的js類jdhcn.js中的一個flashDebug方法2009-01-01