假设底层Javascript引擎中存在Harmony Proxies,那么如何构造一个CoffeeScript超类,以便扩展它可以让一个类定义一个noSuchMethod方法(或methodMessing)?
如果类(或其超类)没有请求的方法,那么将使用名称和参数列表调用该方法。
答案 0 :(得分:4)
(注意:我只在Firefox中对此进行了测试,因为它似乎是唯一支持Harmony代理的浏览器。)
这似乎适用于缺少属性:
class DynamicObject
propertyMissingHandler =
get: (target, name) ->
if name of target
target[name]
else
target.propertyMissing name
constructor: ->
return new Proxy @, propertyMissingHandler
# By default return undefined like a normal JS object.
propertyMissing: -> undefined
class Repeater extends DynamicObject
exited: no
propertyMissing: (name) ->
if @exited then "#{name.toUpperCase()}!" else name
r = new Repeater
console.log r.hi # -> hi
console.log r.exited # -> false. Doesn't print "exited" ;)
r.exited = yes
console.log r.omg # -> OMG!
现在,它有效,但它有一个小的大警告:它依赖于“其他类型”构造函数。也就是说,DynamicObject 的构造函数返回而不是DynamicObject实例(它返回包装实例的代理)。其他类型的构造函数具有细微且不那么微妙的问题they are not a very loved feature in the CoffeeScript community。
例如,上面的工作(在CoffeeScript 1.4中),但只是因为生成的Repeater构造函数返回调用超级构造函数的结果(因此返回代理对象)。如果Repeater有不同的构造函数,它将无法工作:
class Repeater extends DynamicObject
# Innocent looking constructor.
constructor: (exited = no) ->
@exited = exited
propertyMissing: (name) ->
if @exited then "#{name.toUpperCase()}!" else name
console.log (new Repeater yes).hello # -> undefined :(
您必须显式返回调用超级构造函数的结果才能使其工作:
constructor: (exited = no) ->
@exited = exited
return super
所以,由于其他类型的构造函数有点令人困惑/破坏,我建议避免它们并使用类方法来实例化这些对象而不是new
:
class DynamicObject
propertyMissingHandler =
get: (target, name) ->
if name of target
target[name]
else
target.propertyMissing name
# Use create instead of 'new'.
@create = (args...) ->
instance = new @ args...
new Proxy instance, propertyMissingHandler
# By default return undefined like a normal JS object.
propertyMissing: -> undefined
class Repeater extends DynamicObject
constructor: (exited = no) ->
@exited = exited
# No need to worry about 'return'
propertyMissing: (name) ->
if @exited then "#{name.toUpperCase()}!" else name
console.log (Repeater.create yes).hello # -> HELLO!
现在,对于缺少方法,为了使问题中的请求具有相同的接口,我们可以在代理处理程序中执行类似的操作,而不是直接调用特殊方法(propertyMissing)在目标没有具有该名称的属性时,它返回一个函数,该函数又调用特殊方法(methodMissing):
class DynamicObject2
methodMissingHandler =
get: (target, name) ->
return target[name] if name of target
(args...) ->
target.methodMissing name, args
# Use this instead of 'new'.
@create = (args...) ->
instance = new @ args...
new Proxy instance, methodMissingHandler
# By default behave somewhat similar to normal missing method calls.
methodMissing: (name) -> throw new TypeError "#{name} is not a function"
class CommandLine extends DynamicObject2
cd: (path) ->
# Usually 'cd' is not a program on its own.
console.log "Changing path to #{path}" # TODO implement me
methodMissing: (name, args) ->
command = "#{name} #{args.join ' '}"
console.log "Executing command '#{command}'"
cl = CommandLine.create()
cl.cd '/home/bob/coffee-example' # -> Changing path to /home/bob/coffee-example
cl.coffee '-wc', 'example.coffee' # -> Executing command 'coffee -wc example.coffee'
cl.rm '-rf', '*.js' # -> Executing command 'rm -rf *.js'
不幸的是,我找不到一种方法来区分属性访问和代理处理程序中的方法调用,以便DynamicObject可以更智能,并相应地调用propertyMissing或methodMissing(虽然方法调用只是一个属性,但它有意义)访问后跟函数调用)。
如果我必须选择并使DynamicObject尽可能灵活,我将使用propertyMissing实现,因为子类可以选择他们想要如何实现propertyMissing并将该缺失属性视为方法。上面根据propertyMissing实现的CommandLine示例将是:
class CommandLine extends DynamicObject
cd: (path) ->
# Usually 'cd' is not a program on its own.
console.log "Changing path to #{path}" # TODO implement me
propertyMissing: (name) ->
(args...) ->
command = "#{name} #{args.join ' '}"
console.log "Executing command '#{command}'"
有了这个,我们现在可以混合从同一个基类继承的Repeater和CommandLines(多么有用!= P):
cl = CommandLine.create()
r = Repeater.create yes
cl.echo r['hello proxies'] # -> Executing command 'echo HELLO PROXIES!'