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
. Useconsole.log
orconsole.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
1 Answer
Reset to default 13Just 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
版权声明:本文标题:How to inject javascript code to the beginning of every prototype method? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741265279a2368345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论