开发者

Is it possible to override the "call" function?

Is it possible to override the "call" function on开发者_StackOverflow中文版 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)" ?


Sounds 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜