admin管理员组

文章数量:1287881

I want to inject code in javascript, for debugging purposes, within every one of my method prototypes in javascript. This example shows one class only, but assume I have hundreds of classes and each class has dozens of methods. This mechanism should perform at prototype level without the need to specify each class/method name.

function MyClass1() {
    this.attrib = "ABC";
}

MyClass1.prototype.myMethod = function() {
   alert("first row");  // <---- THE INJECTION SHOULD OCCUR BEFORE THIS LINE OF CODE
}

The idea is to dynamically inject some code before the first row of myMethod(), during the first loading/execution of the javascript code. Such as:

MyClass1.prototype.myMethod = function() {
   alert("I was injected dynamically");
   alert("first row");
}

So for every other Class and Method, the same should happen. Is this achievable using the Function.prototype approach ?

I want to inject code in javascript, for debugging purposes, within every one of my method prototypes in javascript. This example shows one class only, but assume I have hundreds of classes and each class has dozens of methods. This mechanism should perform at prototype level without the need to specify each class/method name.

function MyClass1() {
    this.attrib = "ABC";
}

MyClass1.prototype.myMethod = function() {
   alert("first row");  // <---- THE INJECTION SHOULD OCCUR BEFORE THIS LINE OF CODE
}

The idea is to dynamically inject some code before the first row of myMethod(), during the first loading/execution of the javascript code. Such as:

MyClass1.prototype.myMethod = function() {
   alert("I was injected dynamically");
   alert("first row");
}

So for every other Class and Method, the same should happen. Is this achievable using the Function.prototype approach ?

Share Improve this question edited Jul 6, 2013 at 12:48 gextra asked Jul 6, 2013 at 12:22 gextragextra 8,91510 gold badges41 silver badges66 bronze badges 5
  • 3 possible duplicate of Need to hook into a javascript function call, any way to do this? – simonzack Commented Jul 6, 2013 at 12:25
  • 1 I hope you're not planning to display debug info using alert. Use console.log or console.debug instead (if supported). – Bart Commented Jul 6, 2013 at 12:30
  • 1 Even console.log can't be relied on. It'll crash Internet Explorer unless the developer tools have been opened. – mzedeler Commented Jul 6, 2013 at 12:31
  • I used "alert" simply for the purpose of this inquiry. The debug that I plan to perform is based on a specific controlling global object. – gextra Commented Jul 6, 2013 at 12:43
  • regarding the duplication, I am trying to perform this dynamically for all classes and all methods. I am ensuring my question specifies this more clearly. thanks – gextra Commented Jul 6, 2013 at 12:46
Add a ment  | 

1 Answer 1

Reset to default 13

Just wrap your method. Here is the standard method:

MyClass1.prototype.myMethod = function() {
    alert("first row");
}

Then wrap it:

var orig = MyClass1.prototype.myMethod;
MyClass1.prototype.myMethod = function() {
    alert('Injected');
    return orig.apply(this, arguments);
}

You are asking two questions, and I've only answered one of them (i.e. how to wrap a function). The other part - how to do this on many functions - is best done using a specialized library. In fact, this can be done using Aspect Oriented Programming (AOP). I found a couple of JavaScript libraries that offers this, one of them is Aop.js (try googling for more yourself).

本文标签: How to inject javascript code to the beginning of every prototype methodStack Overflow