最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS中prototype關(guān)鍵字的功能介紹及使用示例

 更新時間:2013年07月21日 18:06:21   作者:  
prototype 關(guān)鍵字可以為JS原有對象或者自己創(chuàng)建的類中添加方法或者屬性。也可以實現(xiàn)繼承,下面以實例的方式為大家詳細介紹下
prototype 關(guān)鍵字可以為 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 關(guān)鍵字的使用</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 關(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>

相關(guān)文章

最新評論

易门县| 马关县| 边坝县| 冀州市| 望城县| 拉萨市| 石楼县| 邵阳县| 疏附县| 西峡县| 香格里拉县| 浪卡子县| 汶川县| 陈巴尔虎旗| 任丘市| 建阳市| 井冈山市| 射洪县| 平阴县| 淮安市| 旌德县| 长泰县| 九龙城区| 大石桥市| 兴城市| 昌平区| 永兴县| 吕梁市| 广汉市| 牙克石市| 永靖县| 岑溪市| 古交市| 莒南县| 敦煌市| 长葛市| 玛沁县| 阿拉善左旗| 五指山市| 大理市| 合阳县|