JS自定義混合Mixin函數(shù)示例
本文實(shí)例講述了JS自定義混合Mixin函數(shù)。分享給大家供大家參考,具體如下:
<script type="text/javascript"> /* 增加函數(shù) */ function augment(receivingClass, givingClass) { for(methodName in givingClass.prototype) { if(!receivingClass.prototype[methodName]) { receivingClass.prototype[methodName] = givingClass.prototype[methodName]; } } } /* 改進(jìn)的增加函數(shù) */ function augment(receivingClass, givingClass) { if(arguments[2]) { // Only give certain methods. for(var i = 2, len = arguments.length; i < len; i++) { receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]]; } } else { // Give all methods. for(methodName in givingClass.prototype) { if(!receivingClass.prototype[methodName]) { receivingClass.prototype[methodName] = givingClass.prototype[methodName]; } } } } var Author = function Author(name, books) { // 構(gòu)造函數(shù) this.name = name; this.books = books || 'default value'; }; Author.prototype = { getName: function() { return this.name; }, getBooks: function() { return this.books; } }; var Editor = function Editor() { }; Editor.prototype = { hello: function() { return 'Hello,'+this.name; } }; augment(Author, Editor); var author = new Author('Ross Harmes', ['JavaScript Design Patterns']); console.log(author.getName()); console.log(author.getBooks()); console.log(author.hello()); </script>
結(jié)果:
經(jīng)過拼接處理之后,author就獲取到了hello方法了,屬性還是自己的name。
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript常用函數(shù)技巧匯總》、《javascript面向?qū)ο笕腴T教程》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- ExtJS4中使用mixins實(shí)現(xiàn)多繼承示例
- 用JavaScript實(shí)現(xiàn)單繼承和多繼承的簡(jiǎn)單方法
- js中繼承的幾種用法總結(jié)(apply,call,prototype)
- js的2種繼承方式詳解
- 實(shí)現(xiàn)JavaScript中繼承的三種方式
- JavaScript是如何實(shí)現(xiàn)繼承的(六種方式)
- 前端開發(fā)必須知道的JS之原型和繼承
- JS繼承--原型鏈繼承和類式繼承
- Javascript 繼承機(jī)制的實(shí)現(xiàn)
- js實(shí)現(xiàn)繼承的5種方式
- 再談javascript原型繼承
- JavaScript mixin實(shí)現(xiàn)多繼承的方法詳解
相關(guān)文章
微信小程序中實(shí)現(xiàn)手指縮放圖片的示例代碼
本篇文章主要介紹了微信小程序中實(shí)現(xiàn)手指縮放圖片的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Javascript中document.referrer隱藏來源的方法
這篇文章主要介紹了Javascript中document.referrer隱藏來源的方法,文中通過一個(gè)實(shí)例給大家介紹了實(shí)現(xiàn)的方法,有需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01JavaScript簡(jiǎn)單實(shí)現(xiàn)鼠標(biāo)移動(dòng)切換圖片的方法
這篇文章主要介紹了JavaScript簡(jiǎn)單實(shí)現(xiàn)鼠標(biāo)移動(dòng)切換圖片的方法,涉及JavaScript針對(duì)鼠標(biāo)事件的響應(yīng)及頁面元素的動(dòng)態(tài)變換技巧,需要的朋友可以參考下2016-02-02JavaScript flash復(fù)制庫(kù)類 Zero Clipboard
開發(fā)中經(jīng)常會(huì)用到復(fù)制的功能,在 IE 下實(shí)現(xiàn)比較簡(jiǎn)單。但要想做到跨瀏覽器比較困難了。2011-01-01Javascript對(duì)象Clone實(shí)例分析
這篇文章主要介紹了Javascript對(duì)象Clone用法,實(shí)例分析了javascript對(duì)象克隆的相關(guān)技巧,需要的朋友可以參考下2015-06-06微信小程序自定義tabbar欄實(shí)現(xiàn)過程講解
tabBar相對(duì)而言用的還是比較多的,但是用起來并沒有難,下面這篇文章主要給大家介紹了關(guān)于微信小程序全局配置之tabBar的相關(guān)資料,文中通過圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02