Javascript原型是否具有与Lua的__index&相同的东西。 __newindex?

时间:2009-10-29 07:52:48

标签: javascript prototype lua

我想在Javascript对象上定义一个行为,当引用的属性/方法不存在时,它会启动。 在Lua中,您可以使用元表和__index & __newindex方法来完成此操作。

--Lua code
o = setmetatable({},{__index=function(self,key)
  print("tried to undefined key",key)
  return nil
end
})

所以我想知道javascript中是否有类似内容。

我想要实现的是一个通用的RPC接口,它的工作原理如下(不是有效的Javascript):

function RPC(url)
{
    this.url = url;
}

RPC.prototype.__index=function(methodname) //imagine that prototype.__index exists
{ 
    AJAX.get(this.url+"?call="+ methodname);
}

var proxy = RPC("http://example.com/rpcinterface");
proxy.ServerMethodA(1,2,3);
proxy.ServerMethodB("abc");

那我怎么能这样做呢?

甚至可以这样做吗?

4 个答案:

答案 0 :(得分:4)

仅供参考:Firefox支持非标准__noSuchMethod__扩展名。

答案 1 :(得分:3)

javascript更像是scheme,而不是像smalltalk(支持未定义的方法)或lua。很遗憾,据我所知,您的请求不受支持。

您可以通过额外的步骤来模拟此行为。

function CAD(object, methodName) // Check and Attach Default
{
    if (!(object[methodName] && typeof object[methodName] == "function") &&
         (object["__index"] && typeof object["__index"] == "function")) {
        object[methodName] = function() { return object.__index(methodName); };
    }
}

所以你的例子变成了

function RPC(url)
{
    this.url = url;
}

RPC.prototype.__index=function(methodname) //imagine that prototype.__index exists
{ 
    AJAX.get(this.url+"?call="+ methodname);
}

var proxy = RPC("http://example.com/rpcinterface");
CAD(proxy, "ServerMethodA");
proxy.ServerMethodA(1,2,3);
CAD(proxy, "ServerMethodB");
proxy.ServerMethodB("abc");

可以在CAD中实现更多功能,但是这给了你一个想法......你甚至可以将它用作调用函数的调用机制,如果它存在的话,绕过我引入的额外步骤。

答案 2 :(得分:1)

我认为您的实际需求比示例更复杂,因为您对传递给ServerMethodAServerMethodB的参数一无所知,否则您只会执行类似< / p>

function RPC(url)
{
    this.url = url;
}

RPC.prototype.callServerMethod = function(methodname, params)
{ 
    AJAX.get(this.url+"?call="+ methodname);
}

var proxy = RPC("http://example.com/rpcinterface");
proxy.callServerMethod("ServerMethodA", [1,2,3]);
proxy.callServerMethod("ServerMethodB", "abc");

答案 3 :(得分:1)

  

问了8年零8个月前

现在我们可以使用“代理”

Proxy - JavaScript | MDN

一种简单的使用方法:

-Lua代码

local o = setmetatable({},{__index=function(self,key)
  print("tried to undefined key",key)
  return nil
end

//在Java中具有代理服务器

let o = new Proxy({}, {
    get: function (target, key, receiver) {
        if (!target.hasOwnProperty(key)){
            console.log("tried to undefined key "+key);
        }
        return Reflect.get(target, key, receiver);
    },
    set: function (target, key, value, receiver) {
        console.log(`set `+ key);
        return Reflect.set(target, key, value, receiver);
    }
})

获取:__index

set:__newindex

Reflect.get:rawget

Reflect.set:rawset

在控制台中:

let o= new Proxy({},{
    get: function (target, key, receiver) {
            let ret = Reflect.get(target, key, receiver);
            if (!target.hasOwnProperty(key)){
                console.log("tried to undefined key "+key);
            }
            return ret;
        }
})
>> undefined

o.a
>> VM383:5 tried to undefined key a
>> undefined

o.a = 1
>> 1

o.a
>> 1
相关问题