JS繼承定義與使用方法簡(jiǎn)單示例
本文實(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測(cè)試上述代碼運(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é)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
el-date-picker?限制開始時(shí)間和結(jié)束時(shí)間的代碼實(shí)現(xiàn)
在Vue.js中使用Element?UI庫(kù)的el-date-picker組件時(shí),可以通過設(shè)置picker-options來限制開始時(shí)間和結(jié)束時(shí)間的選擇范圍,下面通過例子介紹el-date-picker?限制開始時(shí)間和結(jié)束時(shí)間的實(shí)現(xiàn),感興趣的朋友一起看看吧2024-08-08
JS 非圖片動(dòng)態(tài)loading效果實(shí)現(xiàn)代碼
功能說明:譬如在按某個(gè)button時(shí),顯示消息"Loading”,然后每隔一秒后后面加上".",至一定數(shù)量的"."時(shí)如:"Loading...",再重置此消息為"Loading",繼續(xù)動(dòng)態(tài)顯示,直至按鈕事件處理完成。2010-04-04
javascript類型系統(tǒng)_正則表達(dá)式RegExp類型詳解
下面小編就為大家?guī)硪黄猨avascript類型系統(tǒng)_正則表達(dá)式RegExp類型詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
JavaScript 實(shí)現(xiàn)普通數(shù)組數(shù)據(jù)轉(zhuǎn)化為樹形數(shù)據(jù)結(jié)構(gòu)的步驟說明
在 JavaScript 中,將普通數(shù)組數(shù)據(jù)轉(zhuǎn)化為樹形結(jié)構(gòu)的數(shù)據(jù)是一個(gè)常見的任務(wù),特別是在處理層級(jí)數(shù)據(jù)(例如分類、組織結(jié)構(gòu)等)時(shí),本文展示如何將一個(gè)扁平的數(shù)組轉(zhuǎn)化為樹形數(shù)據(jù)結(jié)構(gòu),感興趣的朋友一起看看吧2024-12-12
js獲取客戶端外網(wǎng)ip的簡(jiǎn)單實(shí)例
這篇文章主要介紹了js獲取客戶端外網(wǎng)ip的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-11-11
一文吃透JS樹狀結(jié)構(gòu)的數(shù)據(jù)處理(增刪改查)
在開發(fā)后臺(tái)管理系統(tǒng)時(shí),常常會(huì)涉及到各種樹狀結(jié)構(gòu)的數(shù)據(jù)處理邏輯,例如:增,刪,改,查等。而樹狀結(jié)構(gòu)的處理就沒有數(shù)組那么的直觀。本文為大家總結(jié)了JS樹狀結(jié)構(gòu)的數(shù)據(jù)處理的方法,需要的可以參考下2022-07-07
javascript單頁(yè)面手勢(shì)滑屏切換原理詳解
這篇文章主要為大家詳細(xì)介紹了javascript單頁(yè)面手勢(shì)滑屏切換原理,感興趣的小伙伴們可以參考一下2016-03-03

