我一直在寻找__getattr__
JavaScript 中 Python 的替代品,这里有几个答案提到了 Firefox 的__defineGetter__
. 这不仅不兼容跨浏览器,而且它实际上不允许你做任何真正有用的事情,就像这个伪代码所做的那样:
var api = function()
{
var __getattr__ = function(attr, args)
{
var api_method = attr;
$post(url, api_method, args, function(response){alert(response)});
}
}
// Now api.getprofile('foo', 'spam'); would call the following code behind the scenes:
$post(url, 'getprofile', ['foo', 'spam'], function(response){alert(response)});
显然这是伪代码,但是在 JavaScript 中是否有任何已知的方法可以真正做到这一点?我知道我可以很好地使用手动版本(最后一行代码),但是当你使用大约 30 个函数时,这变得非常乏味。