2018-07-23 14:30:33 1151瀏覽
今天扣丁學堂小編給大家整理了一篇關于Java培訓之JS實現(xiàn)面向?qū)ο罄^承方式的詳解,首先js是門靈活的語言,實現(xiàn)一種功能往往有多種做法,ECMAScript沒有明確的繼承機制,而是通過模仿實現(xiàn)的,根據(jù)js語言的本身的特性,js實現(xiàn)繼承有以下通用的幾種方式:
functionParent(firstname) { this.fname=firstname; this.age=40; this.sayAge=function() { console.log(this.age); } } functionChild(firstname) { this.parent=Parent; this.parent(firstname); deletethis.parent; this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } } varmychild=newChild("李"); mychild.saySomeThing();
functionParent(firstname) { this.fname=firstname; this.age=40; this.sayAge=function() { console.log(this.age); } } functionChild(firstname) { this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } this.getName=function() { returnfirstname; } } varchild=newChild("張"); Parent.call(child,child.getName()); child.saySomeThing();
functionParent(firstname) { this.fname=firstname; this.age=40; this.sayAge=function() { console.log(this.age); } } functionChild(firstname) { this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } this.getName=function() { returnfirstname; } } varchild=newChild("張"); Parent.apply(child,[child.getName()]); child.saySomeThing();
functionParent() { this.sayAge=function() { console.log(this.age); } } functionChild(firstname) { this.fname=firstname; this.age=40; this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } } Child.prototype=newParent(); varchild=newChild("張"); child.saySomeThing();
functionParent() { this.sayAge=function() { console.log(this.age); } } Parent.prototype.sayParent=function() { alert("thisisparentmethod!!!"); } functionChild(firstname) { Parent.call(this); this.fname=firstname; this.age=40; this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } } Child.prototype=newParent(); varchild=newChild("張"); child.saySomeThing(); child.sayParent();
以上就關于扣丁學堂JavaScript實現(xiàn)面向?qū)ο罄^承方式的詳細介紹,希望對大家有所幫助,最后想要了解更多內(nèi)容的小伙伴可以登錄扣丁學堂官網(wǎng)咨詢??鄱W堂不僅有專業(yè)的老師和與時俱進的課程體系,還有大量的Java視頻教程供學員觀看學習,想要快速學習Java開發(fā)技術的小伙伴快快行動吧。
【關注微信公眾號獲取更多學習資料】