在javascript中将对象成员(如this.member)传递给类方法

在javascript中将对象成员(如this.member)传递给类方法,javascript,methods,pass-by-reference,member,Javascript,Methods,Pass By Reference,Member,我正在尝试将this.SOME_成员传递给一个方法,但它不能作为同一个成员通过。下面的代码应该更符合我的意图 function App() { this.canvas = new Object(); } App.prototype.createCanvas = function(obj, width, height) { obj = document.createElement('canvas'); obj.setAttribute('width', width);

我正在尝试将this.SOME_成员传递给一个方法,但它不能作为同一个成员通过。下面的代码应该更符合我的意图

function App()
{
    this.canvas = new Object();
}

App.prototype.createCanvas = function(obj, width, height)
{
    obj = document.createElement('canvas');
    obj.setAttribute('width', width);
    obj.setAttribute('height', height);

    log(obj == this.canvas); //false
    log(this.canvas == app.canvas); //true 
                                    //it's clear that this refers to app

    //so why doesn't obj refers to this.canvas? 
    //how do i get obj to equal this.canvas?
}

App.prototype.init = function()
{
    this.createCanvas(this.canvas, 800, 600);
    log(this.canvas.width); //undefined
}

var app = new App();
app.init();
现在,我明白我可以简单地做以下几件事:

function App()
{
    this.canvas = new Object();
}

App.prototype.createCanvas = function(width, height)
{
    this.canvas = document.createElement('canvas');
    this.canvas.setAttribute('width', width);
    this.canvas.setAttribute('height', height);
}

App.prototype.init = function()
{
    this.createCanvas(800, 600);
    log(this.canvas.width); //800
}

var app = new App();
app.init();
这适用于大多数应用程序,但我仅限于将变量命名为This.canvas。我最终需要的方法是更灵活一点

请记住,我使用的函数document.createElement和element.setAttribute只是一个示例;它们可以替换为任何修改变量obj的内容


所以当“this.canvas”被用作参数时,它会发生什么变化?我将如何实现我期待的目标

提前谢谢

那么为什么obj不引用这个.canvas呢

因为你在函数中做的第一件事是给它一个不同的值:

App.prototype.createCanvas = function(obj, width, height)
{
    obj = document.createElement('canvas');
//  ^------------------------------------------ here
    obj.setAttribute('width', width);
    obj.setAttribute('height', height);

    log(obj == this.canvas); //false
    log(this.canvas == app.canvas); //true 
                                    //it's clear that this refers to app

    //so why doesn't obj refers to this.canvas? 
    //how do i get obj to equal this.canvas?
}
JavaScript是一种纯粹的传递值语言。
obj
参数具有您传入的值(来自
this.canvas
,因为这是您在调用它时给出的值)
obj
不是对
this.canvas
属性的引用。要以您想要的方式执行您想要执行的操作,需要通过引用传递
this.canvas
(属性),这在JavaScript中是无法做到的。(但请参见下文。)

现在,我明白我可以简单地做以下几件事:

function App()
{
    this.canvas = new Object();
}

App.prototype.createCanvas = function(width, height)
{
    this.canvas = document.createElement('canvas');
    this.canvas.setAttribute('width', width);
    this.canvas.setAttribute('height', height);
}

App.prototype.init = function()
{
    this.createCanvas(800, 600);
    log(this.canvas.width); //800
}

var app = new App();
app.init();
(代码省略)

这适用于大多数应用程序,但我仅限于将变量命名为
This.canvas
。我最终需要的方法是更灵活一点

这是一个有点奇怪的要求(当然所有
App
实例都应该在
canvas
中有画布),但是如果您希望能够告诉
createCanvas
对象上要放置画布的属性,您可以这样做:

JavaScript支持两种引用对象属性的方法:使用文字的虚线表示法(
this.canvas
),以及使用字符串的括号表示法(
this[“canvas”]
)。在后一种情况下,字符串不必是文本,它可以是任何表达式的结果,包括变量引用

因此,您的
createCanvas
变成:

App.prototype.createCanvas = function(propName, width, height)
{
    // Create the canvas
    var obj = document.createElement('canvas');
    obj.setAttribute('width', width);
    obj.setAttribute('height', height);

    // Save that canvas to the given property
    this[propName] = obj;
}
你这样称呼它:

App.prototype.init = function()
{
    this.createCanvas("canvas", 800, 600);
}
也就是说,让
createCanvas
返回画布会更直接,这样调用代码就可以将画布放在它想要的任何地方:

App.prototype.createCanvas = function(width, height)
{
    // Create the canvas
    var obj = document.createElement('canvas');
    obj.setAttribute('width', width);
    obj.setAttribute('height', height);
    return obj;
}
然后


旁注:您缺少分号,当您有
x=…
时,它需要一个
结尾,即使
是函数表达式。例如,
App.prototype.init=…
需要一个
结尾处:

App.prototype.init = function()
{
    this.canvas = this.createCanvas(800, 600);
}; // <=== Here
App.prototype.init=function()
{
this.canvas=this.createCanvas(800600);
}; //  你的台词

obj = document.createElement('canvas');
重写传递到
createCanvas
方法中的obj变量,并将其完全设置为新对象。变量分配会打断以前的引用,并创建对新对象的新引用。obj变量是对另一个对象的引用。不能使用它将引用的对象更改为完全不同的对象


如果通过调用方法或更改属性对obj变量进行操作,那么这些都将对原始对象进行操作。因此,可以将
this
对象传递到方法中(尽管这似乎有点毫无意义,除非您计划在不同的范围内调用函数)。

我已经更新了我的答案,很抱歉第一次误读了这个问题。