您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何在JavaScript中繼承定義,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
<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é)果:
上述就是小編為大家分享的如何在JavaScript中繼承定義了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。