Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向javascript对象添加方法并使用对象';s变量_Javascript_Oop_Object_Methods - Fatal编程技术网

如何向javascript对象添加方法并使用对象';s变量

如何向javascript对象添加方法并使用对象';s变量,javascript,oop,object,methods,Javascript,Oop,Object,Methods,我希望能够使用对象的成员变量: 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,

我希望能够使用对象的成员变量:

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,

为什么我不能在displayImage方法中使用
文件
变量,如何声明displayImage以便
文件
变量可供使用?

要访问对象上的任何属性,只需使用:

this.file
this.id
this.{nameOfProperty}

要访问对象上的任何属性,只需使用:

this.file
this.id
this.{nameOfProperty}

没有办法“声明”它以便它可以在所有原型方法中使用-您必须使用
this.file
而不是
file

另一种选择是不使用原型方法:

function Upload(file, filename, id) {
    this.file = file;
    this.filename = filename;
    this.id = id;

    this.displayImage = function(btn) {
        $.canvasResize(file,
            {
                width: 160,
                height: 0,
                crop: false,
                quality: 100,
                callback: function(data)
                {
                    $('#' + btn).css("background", "url(" + data + ")")
                }
            }
        });
    };
}

没有办法“声明”它以便它可以在所有原型方法中使用-您必须使用
this.file
而不是
file

另一种选择是不使用原型方法:

function Upload(file, filename, id) {
    this.file = file;
    this.filename = filename;
    this.id = id;

    this.displayImage = function(btn) {
        $.canvasResize(file,
            {
                width: 160,
                height: 0,
                crop: false,
                quality: 100,
                callback: function(data)
                {
                    $('#' + btn).css("background", "url(" + data + ")")
                }
            }
        });
    };
}

您需要区分变量和属性。构造函数的三个参数(
文件、文件名、id
)是该函数的[local]变量,不能从外部访问


但是,通过为实例(通过引用)指定值,您在实例上创建了属性(具有相同的名称)。在prototype方法中,您只能访问这些属性,因此您需要对它们使用点成员运算符-函数的作用域中没有定义具有该名称的变量(如异常消息明确指出的)。改用
此.file

您需要区分变量和属性。构造函数的三个参数(
文件、文件名、id
)是该函数的[local]变量,不能从外部访问


但是,通过为实例(通过引用)指定值,您在实例上创建了属性(具有相同的名称)。在prototype方法中,您只能访问这些属性,因此您需要对它们使用点成员运算符-函数的作用域中没有定义具有该名称的变量(如异常消息明确指出的)。改为使用
this.file

您想使用
this.file
更一般地说,所有实例变量都需要以
this.something
的形式访问。您想使用
this.file
的可能重复。更一般地说,所有实例变量都需要以
this.something
的形式访问。可能的重复