在JS中覆盖图像构造函数? [英] Override image constructor in JS?
本文介绍了在JS中覆盖图像构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以覆盖JS中的 Image
构造函数?因此,例如,每次创建 new Image()
时,都会向控制台写入一条消息?
(function() {
var OriginalImage = window.Image;
window.Image = function(width,height){
console.log('New image');
返回new OriginalImage(width ,身高);
}
}());
不确定它是否适用于所有浏览器。
无论如何,覆盖内置类型并不是最好的选择(除非你想用它来模拟/存储以进行测试)。
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?
解决方案
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).
这篇关于在JS中覆盖图像构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文