长话短说:我的情况是我喜欢PHP式的getter,但是在JavaScript中.
我的JavaScript只在Firefox中运行,因此我可以使用Mozilla特定的JS.
我可以找到制作JS getter的唯一方法是需要指定它的名称,但是我想为所有可能的名称定义一个getter .我不确定这是否可行,但我非常想知道.
在PHP中,您可以使用"魔术" __call功能检测方法何时被调用,即使它不存在.
public function __call($methodName, $args)
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
您可以调用任何方法,并将名称和参数传递给此魔法catch-all.
在JavaScript中是否有类似的技术允许调用任何方法,即使它实际上不存在于对象上?
var foo = (function () {
return {
__call: function (name, args) { // NOT REAL CODE
alert(name); // "nonExistent"
}
}
}());
foo.nonExistent();
Run Code Online (Sandbox Code Playgroud) JavaScript有吸引力Object.defineProperty.因此,我可以在属性定义了getter random的window通过
Object.defineProperty(window, 'random', {
get: function () {
return Math.random();
}
});
random // Evaluates to a random number
Run Code Online (Sandbox Code Playgroud)
是否可以为给定对象定义"通用getter",而不管对象属性如何?我想做点什么
Object.universalGetter(window, function (propertyName) {
console.log('Accessing property', propertyName, 'of window.');
});
window.Function // Prints "Accessing property Function of window."
Run Code Online (Sandbox Code Playgroud)
我可以在JavaScript中使用"通用getter"吗?
我正在寻找一种方法来处理JavaScript中未定义的方法和属性的调用.
这些类似于PHP魔术方法__call,__ callStatic,__ get.
使用此代码的示例可能是:
var myObject = {};
myObject.__call = function (called, args) {
alert(called);
alert(args);
return(true);
}
myObject.meow("kitty", "miau");
Run Code Online (Sandbox Code Playgroud)
这将导致第一个警告对话框显示"喵",第二个显示"kitty,miau".
Spider-Monkey JavaScript引擎实现__noSuchMethod__JavaScript对象的回调函数.
每当JavaScript尝试执行Object的未定义方法时,都会调用此函数.
我想设置一个Object的回调函数,只要访问或分配Object中的未定义属性,就会调用该函数.
我还没有找到__noSuchProperty__为JavaScript对象实现的函数,如果有任何解决方法可以实现相同的结果,我很好奇.
请考虑以下代码:
var a = {};
a.__defineGetter__("bla", function(){alert(1);return 2;});
alert(a.bla);
Run Code Online (Sandbox Code Playgroud)
它相当于[alert(1);alert(2)]- 即使a.bla未定义.
我想获得相同的结果,但是对于未知的属性(即事先不知道a."bla"将是访问的属性)
我正在努力解决最近出现的问题.假设我们想要并且知道如何在javascript中使用动态getter和setter,更像是php(__ get,__ set)中的那些.但是由于javascript没有全能属性,我们唯一能做的就是提供一个可能的键列表,并迭代只在那些上添加getter和setter,并希望别人都不会来.
但问题到目前为止尚未解决.这样就使我想起了另一个方法是使用一个讨厌的黑客与try和catch,因此只要有一个名字会在一个对象是不确定的使用catch是获取(至少),然后恢复代码,硬,也许毫无意义的事做.但是从这里出现了我的第二个问题,在这样的用途:
console.log(g.someundefinedproperty);
Run Code Online (Sandbox Code Playgroud)
结果将是一个console.log显示的调用undefined,没有异常被抛出.然后它来找我:如果我会使用原始的window.undefinedgetter和setter,毕竟每次我搞砸并拼错一个单词或什么时都必须调用它.
所以我试过了
Object.defineProperty(window, 'undefined', {
get : function ()
{
// functional code, including getting the caller and figuring out
// where we are, and what we have to do... easy :D
console.log('works');
},
set : function ()
{
// some couple more fine hacks here
console.log('this too');
}
});
Run Code Online (Sandbox Code Playgroud)
但不幸的undefined是窗口的属性是configurable : false.尝试的其他黑客正在克隆window除了undefined内部window属性之外的对象.并在新的对象上定义新的 …
有没有办法在Javascript中模拟Smalltalk的doesNotUnderstand或Ruby的method_missing?
我的代码看起来像这样:
obj.foo(); // obj might, or might not have a `foo` method.
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以覆盖obj.foo在我的代码中调用时发生的事情,例如:
obj.foo = function(){ alert ("Hello"); });
obj.onCallNonExistentMethod = function(){ // I know this is imaginary syntax
alert("World");
}
obj.foo(); // alerts "Hello"
delete obj.foo;
obj.foo(); // alerts "World" , would TypeError without the method missing handler.
Run Code Online (Sandbox Code Playgroud)
据我所知,在Ruby中可能是method_missing或const_missing类似的东西.
我可以覆盖在JavaScript中调用不存在的对象方法时发生的事情吗?如果可以,我该怎么办?
目标是验证我提供给用户的API,以便他们可以安全地使用API,我可以更清楚地警告他们错误.
像这样的代码有任何陷阱吗?
var Foo = function() {
this.bar = function() { return 'bar'; };
};
var f = new Foo();
f[0] = 'hi';
f[1] = 'there';
Run Code Online (Sandbox Code Playgroud)
请注意,我正在创建一个具有一些misc属性的新函数对象,然后我将对象视为一个数组.如何将数组值存储在对象中?0和1是否像属性名一样对待?