javascript - Override image constructor in JS? - Stack Overflow

Is it possible to override the Image constructor in JS? So that, for example, every time a new Image()

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?

Share Improve this question asked Jan 3, 2012 at 18:58 FluffyFluffy 28.4k42 gold badges156 silver badges238 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Try 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

相关推荐

  • javascript - Override image constructor in JS? - Stack Overflow

    Is it possible to override the Image constructor in JS? So that, for example, every time a new Image()

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信