将对象分配给“this” [英] Assign object to "this"
问题描述
假设我有一个类和一些静态辅助方法,如下所示:
Say I have a class and some static helper methods like this:
function MyClass (myVar) {
this.myVar = myVar;
this.replaceMe = function (value) {
// this will fail
this = MyClass.staticHelper( value );
return this;
}
this.revealVar = function () {
alert( this.myVar );
}
}
MyClass.staticHelper = function (instance, value) {
return new MyClass( instance.myVar + value );
}
我想做的是这样的事情:
What I want to do is something like this:
var instance = new MyClass( 2 );
instance.revealVar(); // alerts 2
instance.replaceMe( 40 ).revealVar(); // alerts 42
原因是我的课程结构稍微复杂一点,我不知道想要每次手动分配所有内部变量,而是替换整个对象。有一个简单的方法吗?
The reason is that my class has a slightly more complicated structure and I don't want to assign all internal variables manually everytime, but rather replace the entire object. Is there a simple way to do so?
推荐答案
instance.replaceMe (40).revealVar();
提醒42
好的,对于返回MyClass.staticHelper(this,value);
就足够了。问题只是下一次调用 instance.revealVar()
现在是否应该警告2或42 - 如果你想要实例
要改为42,它会变得更复杂:
OK, for that return MyClass.staticHelper(this, value);
would suffice. The question is only whether the next call to instance.revealVar()
should now alert 2 or 42 - if you want instance
to be changed to 42 it gets more complicated:
this = MyClass.staticHelper( value ); // this will fail
...因为 此
不是常见变量,而是 keyword 和 计算当前执行上下文的ThisBinding的值 根据输入功能的方式设置 - 您不能分配给它,你只能在调用函数时设置它。
…because this
is not a common variable, but a keyword and evaluates to the value of the ThisBinding of the current execution context which is set depending on how the function is entered - you cannot assign to it, you can only set it when invoking the function.
我不希望每次手动分配所有内部变量,而是替换整个对象。
I don't want to assign all internal variables manually everytime, but rather replace the entire object.
不幸的是,您必须这样做,而不更改 instance <的属性/ code> object(和闭包隐藏变量)你不会改变
实例
和 revealVar()
将st ay 2.
Unfortunately you have to do so, without changing the properties of instance
object (and the closure-hidden variables) you won't change the instance
and revealVar()
will stay 2.
有一种简单的方法吗?
Is there a simple way to do so?
是的,它可以通过编程方式完成。最简单的方法是致电构造函数(再次) on 当前实例,就像使用 新的
关键字:
Yes, it can be done programmatically. The simplest method would be to call the constructor (again) on the current instance, like it happens when invoked with the new
keyword:
MyClass.call( instance, instance.myVar + value );
然而,你不能像创建一个全新实例的静态函数那样使用它。你把它放在一个静态的方法,然后用 replaceMe
用这个
调用它,或者你直接把它放在 replaceMe
。
Yet you can't use this like the static function which creates a completely new instance. Either you put it in a static method and call that from replaceMe
with this
, or you just put it directly in replaceMe
.
如果你需要一个静态方法,它首先返回一个全新的实例,你也可以使用它通过复制旧实例上的新属性:
If you need a static method that at first returns a completely new instance, you could use that as well by copying the new properties on the old instance:
….replaceMe = function(val) {
var newInst = MyClass.staticHelper(this, val); // new MyClass(this.myVar+val);
for (var prop in newInst)
if (newInst.hasOwnProperty(prop))
this[prop] = newInst[prop];
return this;
};
这意味着要覆盖旧属性,而且旧的闭包现在可以被垃圾收集,因为没有引用再给他们了。
That means overwriting the old attributes, and also the old closures can be garbage-collected now as nothing refers to them any more.
顺便说一句,我建议将你的方法放在原型上,而不是在构造函数中分配它们。
Btw, I'd recommend to put your methods on the prototype instead of assigning them in the constructor.
这篇关于将对象分配给“this”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!