繁体   English   中英

Javascript:当“this”被“call”覆盖时,如何获取对父对象的引用?

[英]Javascript: How to get a reference to the parent object when “this” has been overwritten with “call”?

好的,我现在讨厌Javascript,我希望有人可以帮助我。

我的代码设置如下:

function Obj1() {
    var me = this;

    this.something = "yay";

    this.getThis = function(){
        return me;
    }
}

Obj1.prototype.method = function() {
    return this.something;
};

function Obj2() {
    this.something = "nay";

}
Obj2.prototype.method = function() {
    return this.something;
}; 



var o1 = new Obj1();
var o2 = new Obj2();

document.write(o1.method()); // Returns yay
document.write(o1.method.call(o2));   // Returns nay, but I need "yay" here

(JSFiddle @ http://jsfiddle.net/A9u9K/

我的问题是,我需要在第二种情况下调用Obj1.method,但我绝对无法获得对象的引用:(

我该如何解决这个问题?

编辑:对不起,我的示例代码非常错误:(更新了。我从之前的答案中获取了大部分代码,因为它更好,仍然可以解释我的问题。

更新答案

document.write(o1.method.call(o2)); // Returns nay, but I need "yay" here

你说你想起来了排序,但这个问题答案并不实际此处显示的话,我想我可能会对其进行更新,以显示它。

如果这是method要访问me ,即使它被称为具有不同的this值,你必须定义它像getThis ,作为一个封闭过me

function Obj1() {
    var me = this;

    this.something = "yay";

    this.method = function() {
        return me.something;
    };

    this.getThis = function(){
        return me;
    };
}

function Obj2() {
    this.something = "nay";

}
Obj2.prototype.method = function() {
    return this.something;
};

...或者当然,如果你不需要“something”作为对象的属性,只需在构造函数中创建一个var(一个私有变量 ,就像me一样):

function Obj1() {
    var me = this;
    var something = "yay";

    this.method = function() {
        return something;
    };

    this.getThis = function(){
        return me;
    };
}

function Obj2() {
    this.something = "nay";

}
Obj2.prototype.method = function() {
    return this.something;
};

原答案 :( 问题的修订版1,没有me 。)

但我认为,在创建一个闭包时(就像我在4中所做的那样)Javascript应该保留“this”。

this 完全取决于函数的调用方式,而不是函数的定义; 更多关于这里这里 但是你定义你的getThis函数的方式,你可以使用它关闭构造函数调用的事实来解决这个问题(没有双关语)而不使用this

function Obj1() {
    var me = this;               // <== Use a variable to remember `this`

    this.something = "yay";

    this.method = function() {
        return this.something;
    };

    this.getThis = function(){
        return me;               // <== Return it
    };
}

实例

更多关于闭合和管道使me事情在这里工作。

有参与这一成本,而只是一般在构造函数定义函数的方式:通过创建每个单独的对象Obj1Obj2都有自己的每一个功能的副本。 如果有很多这些对象在运行,这会产生内存影响(但除非你有很多,否则你不必担心,你会得到像me这样的东西和其他私有变量的好处)。 相反,如果您使用分配给原型的函数,则所有实例将共享该函数的单个公共副本。

在您的示例代码中,实际上只需要为每个实例复制getThis函数(因为您依赖于闭包),因此您可以这样做以避免不必要的函数扩散:

function Obj1() {
    var me = this;

    this.something = "yay";

    this.getThis = function(){
        return me;
    };
}
Obj1.prototype.method = function() {
    return this.something;
};

function Obj2() {
    this.something = "nay";

}
Obj2.prototype.method = function() {
    return this.something;
};

问题在于使用范围更改此对象的引用。 相反,如果在闭包中直接使用this,则使用等于此的局部变量,即将Obj1更改为L.

function Obj1() {
    this.something = "yay";
        var that = this;

    this.method = function() {
        return that.something;
    }

    this.getThis = function(){
        return that;
    }
}

解决这个问题的唯一办法是另一个地方架,以保持的值thisObj1和在函数中使用它method()getThis()

function Obj1() {
    var instance = this;

    this.something = "yay";

    this.method = function() {
        return instance.something;
    }

    this.getThis = function(){
        return instance;
    }
}

但我无法理解的是你为什么要这样做( obj1.getThis.call(obj2).method() )? 这明确表示您要将方法getThis()的范围更改为其他内容,然后您尝试解决此用法创建的问题。

你能说出你为什么想要这样的东西吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM