admin管理员组文章数量:1305209
I'm wondering if there is a standard way to re-initialize, or re-construct a class instance without creating a new instance all together.
Let's say I have a TestClass instance:
class TestClass {
constructor() {
this.x=0;
this.y=50;
this.z=200;
}
}
var testClassInstance=new TestClass();
And basically, overtime I tweak some of it's values.
testClassInstance.x+=250;
testClassInstance.y-=20;
Then later on I want to reset all of its values to whatever was defined when the instance was created. I'm wondering if there is a way to then basically reinitialize it, without creating an entirely new instance?
Is something like
testClassInstance.constructor()
safe and reliable?
I'm wondering if there is a standard way to re-initialize, or re-construct a class instance without creating a new instance all together.
Let's say I have a TestClass instance:
class TestClass {
constructor() {
this.x=0;
this.y=50;
this.z=200;
}
}
var testClassInstance=new TestClass();
And basically, overtime I tweak some of it's values.
testClassInstance.x+=250;
testClassInstance.y-=20;
Then later on I want to reset all of its values to whatever was defined when the instance was created. I'm wondering if there is a way to then basically reinitialize it, without creating an entirely new instance?
Is something like
testClassInstance.constructor()
safe and reliable?
Share Improve this question edited Feb 2, 2018 at 0:23 Gage Hendy Ya Boy asked Feb 2, 2018 at 0:11 Gage Hendy Ya BoyGage Hendy Ya Boy 1,5744 gold badges20 silver badges37 bronze badges 6- 1 you could add a function to the class that resets the values and call this instead of constructor() – john Smith Commented Feb 2, 2018 at 0:14
- Why don't you just create another method, let's say reset(), that restores whatever the initial values were. – alanfcm Commented Feb 2, 2018 at 0:14
- There's a nice answer already, so I won't get in the way. I am curious, though, why do you need this functionality? – Zlatko Commented Feb 2, 2018 at 0:16
- Your question is totally unclear, you're talking about class, but you want to reset objects. – Ele Commented Feb 2, 2018 at 0:20
- @Zlatko I should've been more specific. I'm realizing this isn't the actual info I need, but regardless, still useful answers. I'm working with the THREEjs orbit controls, and I need to enable/disable them. But looking at it now, this question isn't relevent to it's syntax. – Gage Hendy Ya Boy Commented Feb 2, 2018 at 0:21
2 Answers
Reset to default 8class TestClass {
constructor() {
this.reset();
}
reset(){
this.x=0;
this.y=50;
this.z=200;
}
}
const myTestClass = new TestClass();
myTestClass.x = 5;
console.log(myTestClass.x); // 5
myTestClass.reset();
console.log(myTestClass.x); // 0
This answer was generated because of the first version of this question.
Your class is never modified. The class is an implementation, what you modify are the instances created using that implementation.
Look this code snippet:
class TestClass {
constructor() {
this.x=0;
this.y=50;
this.z=200;
}
}
var testClassInstance=new TestClass();
testClassInstance.x+=250;
testClassInstance.y-=20;
console.log(testClassInstance.x);
console.log(testClassInstance.y);
var anotherTestClassInstance=new TestClass();
console.log(anotherTestClassInstance.x);
console.log(anotherTestClassInstance.y);
See? the new object has the initial values declared in TestClass's constructor.本文标签: javascriptReinitialize a class instance without creating a new instanceStack Overflow
版权声明:本文标题:javascript - Re-initialize a class instance without creating a new instance? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741796379a2397960.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论