admin管理员组

文章数量:1313242

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 method call on Object.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
Add a ment  | 

2 Answers 2

Reset to default 8

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.

本文标签: javascriptIs it possible to override the quotcallquot functionStack Overflow