Override image designer in JS?

Is it possible to override the constructor Imagein JS? So, for example, every time it is created new Image(), a message is written to the console?

+3
source share
2 answers

Try the following:

(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.

In any case, it is better not to override the built-in types (unless you want to use it for bullying / stub for testing purposes).

+6
source

Look at this link, you can override the constructors. However, I believe that now this is what you want, you want to expand it. Take a look at the “Extends ABC” section.

+1
source

All Articles