Javascript 扩展时如何合并而不是替换对象
Javascript 扩展时如何合并而不是替换对象,javascript,inheritance,coffeescript,mixins,Javascript,Inheritance,Coffeescript,Mixins,(新B).loadFunctions将仅是具有loadLockDDR属性的对象 我希望(新B).loadFunctions为{loadDrillingCharges:->,loadlockdrr:->} 我可以扩展(B::loadFunctions,A::loadFunctions),但它并不优雅 我试着用它,但它把super搞砸了 扩展后如何合并对象而不破坏coffescript super。coffescript本机不支持混合,原因是 它们可以自己实现。例如,这里有两个函数, extend(
(新B).loadFunctions
将仅是具有loadLockDDR
属性的对象
我希望(新B).loadFunctions
为{loadDrillingCharges:->,loadlockdrr:->}
我可以扩展(B::loadFunctions,A::loadFunctions)
,但它并不优雅
我试着用它,但它把super搞砸了
扩展后如何合并对象而不破坏coffescript super。coffescript本机不支持混合,原因是 它们可以自己实现。例如,这里有两个函数, extend()和include(),它们将分别向类添加类和实例属性: 扩展=(对象,混合)-> obj[name]=名称的方法,mixin的方法 obj include=(klass,mixin)-> 扩展klass.prototype,mixin #用法 包括鹦鹉, 是的 (新鹦鹉)死了 不是很性感,但是 extend = (obj, mixin) -> obj[name] = method for name, method of mixin obj include = (klass, mixin) -> extend klass.prototype, mixin # Usage include Parrot, isDeceased: true (new Parrot).isDeceased 制作:
class A
constructor: ->
# dosomething
loadFunctions: ->
loadDrillingCharges: (memoize) ->
class B extends A
constructor: ->
super()
loadFunctions: ->
do (that=super()) ->
that.loadLockDDR = (memoize) ->
that
console.log (new A).loadFunctions()
console.log (new B).loadFunctions()
我只想避免使用这种模式,并使用前缀,您已经在使用
load
了。
class A
constructor: ->
# dosomething
loadFunctions: ->
loadDrillingCharges: (memoize) ->
class B extends A
constructor: ->
super()
loadFunctions: ->
do (that=super()) ->
that.loadLockDDR = (memoize) ->
that
console.log (new A).loadFunctions()
console.log (new B).loadFunctions()
{ loadDrillingCharges: [Function] }
{ loadDrillingCharges: [Function], loadLockDDR: [Function] }