JS繼承定義與使用方法簡單示例
本文實(shí)例講述了JS繼承定義與使用方法。分享給大家供大家參考,具體如下:
<script> function Enemy() { this.level = 50; console.log("Enemy constructor"); } Enemy.prototype.attack_play = function(){ console.log("attack_play"); }; Enemy.prototype.wudiai = 100; Enemy.wudiai = "1213"; Enemy.gongji = function(){ console.log("gongji asasasd "+ Enemy.wudiai); } function BossEnemy(){ Enemy.call(this); console.log("Boss constructor"); } // 寫法1 // BossEnemy.prototype = {constructor: BossEnemy,}; // for(var i in Enemy.prototype){ // BossEnemy.prototype[i] = Enemy.prototype[i]; // } // 寫法2 var a = function (){}; a.prototype = Enemy.prototype; BossEnemy.prototype = new a(); BossEnemy.prototype.boss_attack = function(){ console.log("boss_attack"); }; BossEnemy.staticFunc = function(){ console.log("staticFunc called!"); }; var bos = new BossEnemy(); bos.boss_attack(); bos.attack_play(); BossEnemy.staticFunc(); console.log("=========================="); BossEnemy.prototype.attack_play = function(){ Enemy.prototype.attack_play.call(this); console.log("BossEnemy attack play!"); } bos.attack_play(); console.log("*****************************"); // 寫法三 js6 class BingEnemy extends Enemy{ constructor(){ super(); this.flag = true; this.name = "通天教主"; this.level = 100; } static staticFunc(){ console.log("static func called!"); } get BingName(){ return this.name; } set BingName(value){ this.name = value; } }; BingEnemy.haha ="123"; let bing = new BingEnemy(); console.log(bing); BingEnemy.staticFunc(); bing.attack_play(); console.log(bing.BingName); bing.BingName = "jade"; console.log(bing.BingName); //console.log(BingEnemy.wudi); console.log("============================"); </script>
運(yùn)行結(jié)果:
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運(yùn)行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript常用函數(shù)技巧匯總》、《javascript面向?qū)ο笕腴T教程》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
Json實(shí)現(xiàn)傳值到后臺(tái)代碼實(shí)例
這篇文章主要介紹了Json實(shí)現(xiàn)傳值到后臺(tái)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06javascript使用正則獲取url上的某個(gè)參數(shù)
使用indexOf取得?之后的參數(shù),以&使split進(jìn)行分割成數(shù)組,下面展示了一個(gè)從url上獲取名為MenuCode參數(shù)的過程2014-09-09javascript 裝載iframe子頁面,自適應(yīng)高度
2009-03-03JS實(shí)現(xiàn)探測網(wǎng)站鏈接的方法【測試可用】
這篇文章主要介紹了JS實(shí)現(xiàn)探測網(wǎng)站鏈接的方法,通過網(wǎng)站返回錯(cuò)誤響應(yīng)觸發(fā)onerror時(shí)間來判斷網(wǎng)站鏈接的可用性,非常簡便實(shí)用,需要的朋友可以參考下2016-11-11實(shí)用的JS正則表達(dá)式(手機(jī)號(hào)碼/IP正則/郵編正則/電話等)
實(shí)用的JS正則表達(dá)式(手機(jī)號(hào)碼/IP正則/郵編正則/電話等),經(jīng)驗(yàn)積累,感興趣的朋友可以了解下,一定會(huì)對你有幫助的2013-01-01將json轉(zhuǎn)換成struts參數(shù)的方法
下面小編就為大家?guī)硪黄獙son轉(zhuǎn)換成struts參數(shù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Chrome下ifame父窗口調(diào)用子窗口的問題示例探討
這篇文章主要介紹了Chrome下ifame父窗口調(diào)用子窗口的問題,需要的朋友可以參考下2014-03-03