本文实例讲述了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>
运行结果:
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试上述代码运行效果。
更多关于JavaScript相关内容可查看本站专题:《JavaScript常用函数技巧汇总》、《javascript面向对象入门教程》、《JavaScript查找算法技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
标签:
JS,继承
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“JS继承定义与使用方法简单示例”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。