【发布时间】:2013-05-08 14:31:38
【问题描述】:
有没有办法在 javascript 中实现 python 的 __getattribute__()(或 __getattr__())的功能?也就是当一个对象被调用时调用的方法名或属性名无法解析?
例如,实现以下任一功能的机制:
# Python's method syntax
o.Name(args) # o.__getattr__('Name') is called and returns
# a function which is called with args
# Alternative method syntax
o.Name(args) # o.__getattr__('Name', args) is called and returns a value
# Properties syntax
o.Name = v # o.__getattr__('Name', v) is called
v = o.Name # o.__getattr__('Name') is called and returns a value
我对方法语法最感兴趣,但属性语法将是一个不错的奖励。谢谢!
【问题讨论】:
标签: javascript python dynamic duck-typing