【发布时间】:2015-07-11 07:11:04
【问题描述】:
我要创建自定义图像构造函数:
var image1 = new imgConstructor("picture.png", 100, 50);
我试过了:
var imgConstructor = function(src, width, height) {
this = new Image(width, height);
this.src = src;
}
但是this = new Image()无效。
我知道我可以使用工厂函数来做到这一点:
var imgConstructor = function(src, width, height) {
var img = new Image(width, height);
img.src = src;
return img;
}
var image1 = imgConstructor("picture.png", 100, 50);
但我想使用构造函数,使用 "new"。有什么想法吗?
【问题讨论】:
标签: javascript image object constructor src