Javascript 保存这个。image.onLoad中的变量
在本例中,如何将这些变量保存为InfoImage变量?我知道,使用Javascript 保存这个。image.onLoad中的变量,javascript,object,Javascript,Object,在本例中,如何将这些变量保存为InfoImage变量?我知道,使用此将影响图像而不是InfoImage…如果我理解正确,通常的答案是使用变量来引用此,它初始化然后关闭: function InfoImage(path,title){ this.path = path; this.title = title; this.color = undefined; this.maxPixels = undefined; this.init = function()
此
将影响图像而不是InfoImage…如果我理解正确,通常的答案是使用变量来引用此
,它初始化
然后关闭:
function InfoImage(path,title){
this.path = path;
this.title = title;
this.color = undefined;
this.maxPixels = undefined;
this.init = function(){
var canvas = document.querySelector("canvas");
var img_Color = new Image_Processing_Color(canvas);
var img = new Image();
img.onload = function () {
img_Color.init(img);
this.color = img_Color.getDominantColor(); //this doesnt work
this.maxPixels = img_Color.getDominantColorPixels();
};
img.src = path;
};
this.init();
}
使用下一个版本的JavaScript ES6,您将能够使用arrow函数,因为与普通函数不同,arrow函数从定义它们的上下文继承它们的这个值:
function InfoImage(path,title){
this.path = path;
this.title = title;
this.color = undefined;
this.maxPixels = undefined;
this.init = function(){
var canvas = document.querySelector("canvas");
var img_Color = new Image_Processing_Color(canvas);
var img = new Image();
img.onload = function () {
img_Color.init(img);
this.color = img_Color.getDominantColor();
this.maxPixels = img_Color.getDominantColorPixels();
}.bind(this); // <===
img.src = path;
};
}
函数信息图像(路径、标题){
this.path=path;
this.title=标题;
this.color=未定义;
this.maxPixels=未定义;
this.init=函数(){
var canvas=document.querySelector(“canvas”);
var img_Color=新图像处理颜色(画布);
var img=新图像();
img.onload=()=>{/您需要这个
和那个
模式:
function InfoImage(path,title){
this.path = path;
this.title = title;
this.color = undefined;
this.maxPixels = undefined;
this.init = function(){
var canvas = document.querySelector("canvas");
var img_Color = new Image_Processing_Color(canvas);
var img = new Image();
img.onload = () => { // <===
img_Color.init(img);
this.color = img_Color.getDominantColor();
this.maxPixels = img_Color.getDominantColorPixels();
};
img.src = path;
};
}
这很简单。这是一个关键字,取决于函数的绑定调用上下文,但它可以像JavaScript中的任何其他内容一样存储到变量。是的,这正是我想要的,谢谢。
function InfoImage(path,title){
this.path = path;
this.title = title;
this.color = undefined;
this.maxPixels = undefined;
this.init = function(){
var canvas = document.querySelector("canvas");
var img_Color = new Image_Processing_Color(canvas);
var img = new Image();
img.onload = () => { // <===
img_Color.init(img);
this.color = img_Color.getDominantColor();
this.maxPixels = img_Color.getDominantColorPixels();
};
img.src = path;
};
}
function InfoImage(path, title) {
var _this = this;
this.init = function(){
var img = new Image();
img.onload = function () {
_this.color = img_Color.getDominantColor();
};
};
};
function InfoImage(path,title){
var self = this;
this.path = path;
this.title = title;
this.color = undefined;
this.maxPixels = undefined;
this.init = function(){
var canvas = document.querySelector("canvas");
var img_Color = new Image_Processing_Color(canvas);
var img = new Image();
img.onload = function () {
img_Color.init(img);
self.color = img_Color.getDominantColor(); //this doesnt work
self.maxPixels = img_Color.getDominantColorPixels();
};
img.src = path;
};
this.init();
}