admin管理员组文章数量:1313054
Is it possible to override the "call" function on a generic level, so that every time when a method gets called anywhere in the app, something happens.
I tried overriding Object.call, but although I managed to do it, it didn't change anything in the way my app works.
BTW, even if it works, should I explicitly call "foo.call(this,args)" every time, or normal function calls will also work "foo(args)" ?
Is it possible to override the "call" function on a generic level, so that every time when a method gets called anywhere in the app, something happens.
I tried overriding Object.call, but although I managed to do it, it didn't change anything in the way my app works.
BTW, even if it works, should I explicitly call "foo.call(this,args)" every time, or normal function calls will also work "foo(args)" ?
Share Improve this question asked Sep 7, 2011 at 6:51 user802232user802232 2,6117 gold badges37 silver badges41 bronze badges 2-
Are you thinking of
Function.call
? There is no methodcall
onObject.prototype
. – Ray Toal Commented Sep 7, 2011 at 6:55 - Perhaps you can explain what you're really trying to acplish. I don't think you can intercept every javascript function call at the javascript level. You'd probably have to have access to some hooks inside the JS engine to do that. – jfriend00 Commented Sep 7, 2011 at 6:55
2 Answers
Reset to default 8Sounds like you want to do some kind of aspect oriented programming here....
JavaScript, as an ECMAScript dialect, does have the notion of a callable object. Every callable object has an internal property called [[Call]]
. This property is described in Section 8.6.2, Table 9, of the ECMA-262 Specification, 5th edition. It says:
Executes code associated with the object. Invoked via a function call expression. The arguments to the SpecOp are a this object and a list containing the arguments passed to the function call expression. Objects that implement this internal method are callable. Only callable objects that are host objects may return Reference values.
But the thing to be aware of is that [[Call]]
is an internal property, for which the spec says:
An internal property has no name and is not directly accessible via ECMAScript language operators. Internal properties exist purely for specification purposes.
So you can not hook into this mechanism in your own JavaScript code.
Now there are two methods defined in Function.prototype
, apply
and call
. It is true that if you change the definition of Function.prototype.call
then if you create your own function f
, then f.call
will indeed (unless overridden in f's prototype or in f itself) execute that code. This will, as you presumed, NOT automatically happen by calling f
directly. You have to explicitly call the call
method.
All that said, it is best not to muck with predefined, standard methods in the prototypes of built-in objects. A lot of existing code in libraries and applications depend on Function.prototype.call
. Don't mess with it. You can, of course, implement a kind of AOP behavior in many ways. One is to add to Function.prototype
some other method, but don't do this either. Another is to write your own call method with before and after hooks:
function call(theThis, before, main, after, beforeArgs, mainArgs, afterArgs) {
before.apply(theThis, beforeArgs);
main.apply(theThis, mainArgs);
after.apply(theThis. afterArgs);
}
In other words, you'd like to intercept any function call that is done your application? There's no way to do that, at least not on some generic level.
本文标签: javascriptIs it possible to override the quotcallquot functionStack Overflow
版权声明:本文标题:javascript - Is it possible to override the "call" function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741884109a2402918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论