我希望能夠使用一個對象的成員變量:你如何添加方法添加到JavaScript對象,並使用對象的變量
function Upload(file, filename, id){
this.file=file
this.filename=filename
this.id=id;
};
Upload.prototype.displayImage = function(btn){
$.canvasResize(file,
{
width: 160,
height: 0,
crop: false,
quality: 100,
callback: function (data)
{
$('#'+btn).css("background", "url("+data+")")
}
});
};
我訪問這樣的對象和方法:
var upload = new Upload(frontPic, frontPicName, id);
upload.displayImage("btnFrontUploadShow");
但是我得到的錯誤:
ReferenceError: file is not defined
$.canvasResize(file,
爲什麼我不能用file
變量日e displayImage方法,以及如何聲明displayImage以使file
變量可供使用?
你想使用'this.file' – 2013-03-03 15:08:36
更一般地說,所有的實例變量都需要作爲'this.something'訪問。 – Xymostech 2013-03-03 15:09:15
可能重複[Javascript:我是否需要將this.var放置在對象中的每個變量?](http://stackoverflow.com/questions/13418669/javascript-do-i-need-to-put-this- VAR換每可變功能於一個對象) – Bergi 2013-03-03 15:14:57