Is it possible to override the Image
constructor in JS? So that, for example, every time a new Image()
is created, a message is written to the console?
Is it possible to override the Image
constructor in JS? So that, for example, every time a new Image()
is created, a message is written to the console?
2 Answers
Reset to default 7Try this:
(function () {
var OriginalImage = window.Image;
window.Image = function (width, height) {
console.log('New image');
return new OriginalImage(width, height);
}
}());
Not sure if it will work in all browsers.
Anyway it is not best idea to override built in types (unless you want to use it to mock/stub for test purposes).
Take a look at this link, it is possible to override constructors. However, I believe this is now what you want, you want to EXTEND it. Take a look at the "Extends ABC" part.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745249623a4618599.html
评论列表(0条)