Javascript 对于属性,是否有一种与_unosuchmethod _;功能等效的方法,或者有一种在JS中实现它的方法?

Javascript 对于属性,是否有一种与_unosuchmethod _;功能等效的方法,或者有一种在JS中实现它的方法?,javascript,Javascript,在一些javascript实现Rhino、SpiderMonkey中有一个noSuchMethod特性 proxy = { __noSuchMethod__: function(methodName, args){ return "The " + methodName + " method isn't implemented yet. HINT: I accept cash and beer bribes" ; }, realMethod: functi

在一些javascript实现Rhino、SpiderMonkey中有一个noSuchMethod特性

proxy = {
    __noSuchMethod__: function(methodName, args){
        return "The " + methodName + " method isn't implemented yet. HINT: I accept cash and beer bribes" ;
    },

    realMethod: function(){
     return "implemented" ;   
    }
}

js> proxy.realMethod()
implemented
js> proxy.newIPod()
The newIPod method isn't implemented yet. HINT: I accept cash and beer bribes
js>
我想知道,有没有一种方法可以为房产做类似的事情?我想编写可以在属性和方法上分派的代理类。

更新:ECMAScript 6代理是可用的。基本上,如果您不需要支持IE11,您可以使用它们

代理对象允许您为基本操作定义自定义行为,如属性查找、赋值、枚举、函数调用等

使用ES6代理模拟“nosuch方法” 通过在属性访问上实现陷阱,您可以模拟非标准_unosuchmethod__;陷阱的行为:

函数enablenoschmethodobj{ 返回新的Proxyobj{ gettarget,p{ 如果p在目标中{ 返回目标[p]; }否则,如果目标的类型为.\uuuuNoSuchMethod\uuuuuu==函数{ 返回函数…args{ 返回target.\uuuu noSuchMethod\uuuuu.calltarget,p,args; }; } } }; } //用法示例: 功能假人{ this.ownProp1=值1; 返回enablenoschmethodthis; } Dummy.prototype.test=功能{ 调用console.logTest; }; Dummy.prototype.\uuuuNoSuchMethod\uuuuu=functionname,args{ log`没有使用${args}调用这样的方法${name}'; 回来 }; var实例=新的虚拟对象; console.loginstance.ownProp1; 实例测试; instance.someName1,2; 实例xyz3,4;
实例.doesNotExista,b 我认为这种元编程在javascript中还不可能实现。相反,请尝试使用_unosuchmethod _;功能来实现属性getter的效果。不是跨浏览器,因为它是一个


除了SpiderMonkey中的uuu nosuch方法之外,还有uuu defineGetter uuuuuuu、uuu defineSetter uuuu、uuuuu lookupGetter uuuu和uuu lookupSetter uu

下面是如何获得类似于_nosuch方法的行为__

首先,这里有一个简单的对象和一个方法:

var myObject = {
    existingMethod: function (param) {
        console.log('existing method was called', param);
    }
}
现在创建一个代理,它将捕获对属性/方法的访问,并将现有对象添加为第一个参数

var myObjectProxy = new Proxy(myObject, {
   get: function (func, name) {
       // if property or method exists, return it
       if( name in myObject ) {
           return myObject[name];
       }
       // if it doesn't exists handle non-existing name however you choose
       return function (args) {
           console.log(name, args);
       }
    }
});
现在试一试:

myObjectProxy.existingMethod('was called here');
myObjectProxy.nonExistingMethod('with a parameter');
在Chrome/Firefox/Opera中工作。在IEF中不起作用,但在Edge中已起作用。也在手机Chrome上进行了测试

代理的创建可以自动化且不可见,即,如果您使用Factory模式构建对象。我这样做是为了创建可以直接从主线程调用内部函数的worker。多亏了这个名为代理的酷新功能,现在使用workers变得如此简单。有史以来最简单的worker实现:

var testWorker = createWorker('pathTo/testWorker.js');
testWorker.aFunctionInsideWorker(params, function (result) {
    console.log('results from worker: ', result);
});
你可以使用这个类


虽然这是一个老问题,但我今天一直在研究这个问题。我希望能够无缝集成来自另一个上下文(可能是不同的网页或服务器)的代码

从长远来看,这是一种突破,但我认为这仍然是一个有趣的概念。这些东西对于快速地将代码混合在一起非常有用,然后这些代码就存在了很多年,埋在某个地方

var mod=modproxy; mod.callme.first.现在是“你好”,“世界”; mod.hello.world.plot=555; var v=mod.peter.piper.lucky.john.valueOf; console.logv; mod.hello.world=functionv{ alertv; 返回777; }; var v=mod.hello.world'funky; console.logv; var v=mod.hello.world.plot.valueOf; console.logv; mod.www.a99; mod.www.b98; 函数modproxy\uuu notfound\uu{ var mem={}; 返回新代理; 函数gettertarget、名称、接收器、lname{ ifname=='valueOf'{ lname=lname.1; 内存中的名称{ var v=mem[lname]; log`rd:${lname}-${v}`; 返回v; } console.log`rd未找到:${lname}`; 回来 } lname+='.'+名称; 返回newproxy=>{},lname; }//getter 函数setterobj、prop、newval、lname{ lname+='.'+prop; lname=lname.1; log`wt:${lname}-${newval}`; mem[lname]=newval; }//塞特 函数applyertarget、thisArg、args、lname{ lname=lname.1; 内存中的名称{ var v=mem[lname]; iftypeof v===“函数”{ log`fn:${lname}-[${args}]`; 返回v.applythisArg,args; } 返回v; } console.log`fn未找到:${lname}-[${args}]`; }//阿普勒 函数newproxytarget,lname{ target=target |{}; lname=lname | |; 返回新的Proxytarget{ get:target、name、receiver=>{ 返回gettertarget、name、receiver、lname; }, set:target,name,newval=>{ 返回settertarget、name、newval、lname; }, 应用:target、thisArg、args=>{ 返回applyertarget、thisArg、args、lname; } }; }//代理
}//modproxy你找到解决方案了吗?这个问题更多的是出于好奇,而不是出于需要。我试图使用Rhino作为Java应用程序的脚本引擎,这涉及到为宿主对象及其方法创建js包装器,而且是正确的
领带。最后,我改用Clojure,因为它使与Java的交流变得更加容易,不过顺便说一句,在Clojure中创建动态代理实际上比在Javascript中更难。相关:我还使用新的代理API在那里发布了一个答案。Brendan Eich视频似乎不再在JSConf.eu网站上提供。Chrome也有代理。虽然答案很广泛,但完全忽略了一个事实,即问题的重点是调用方法而不是访问属性!不一样。@ValeriuPaloş:我添加了一个示例,说明如何通过使用ES6代理来模拟非标准的“noSuchMethod”陷阱。
var testWorker = createWorker('pathTo/testWorker.js');
testWorker.aFunctionInsideWorker(params, function (result) {
    console.log('results from worker: ', result);
});
var myObj = {
    someAttr: 'foo'
};

var p = new Proxy(myObj, {
    get: function (target, propName) {
        // target is the first argument passed into new Proxy,
        // in this case target === myObj
        return 'myObj with someAttr:"' + target.someAttr 
               + '" had "' + propName 
               + '" called on it.';
    }
});
console.log(p.nonExsistantProperty);
// outputs: 
// myObj with someAttr:"foo" had "nonExsistantProperty" called on it