在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 ×1