我有一个getMeta(url)函数,用于通过链接定义图像的宽度和高度。 它返回带有“ w”和“ h”值的对象。
为什么函数仅返回对象,而不返回值?下面是示例。
popup: {
dimentions: {},
testFunc: function testFunc(url) {
this.dimentions = this.getMeta(url);
console.log(this.dimentions); //browser returns {} h: 1920 w: 720
console.log(this.dimentions.w); //browser returns undefined
},
getMeta: function getMeta(url){
var meta = {};
var img = new Image();
img.onload = function(){
meta.w = this.width;
meta.h = this.height;
};
img.src = url;
return meta; //browser returns {} h: 1920 w: 720
},
}
好的,我做一个回调函数 如何从回调函数获取图像的尺寸?
示例:
testFunc: function testFunc(url) {
this.getMeta(url, function() {
this.dimensions = .... //how can i find dimensions from a callback function?
});
console.log("console log: " + this.dimentions);
},
getMeta: function getMeta(url, cb){
var meta = {};
var img = new Image();
img.onload = cb;
img.src = url;
},