我有一个像这样创建的类:
function T() {
this.run = function() {
if (typeof this.i === 'undefined')
this.i = 0;
if (this.i > 10) {
// Destroy this instance
}
else {
var t = this;
this.i++;
setTimeout( function() {
t.run();
}, 1000);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我初始化它就像 var x = new T();
如果达到10次迭代,我不确定如何从内部破坏这个实例.
此外,我不知道如何在外部销毁它,以防我想在它达到10之前停止它.
在 Chrome 控制台中:
# One
class A {
constructor(x) { this.x = x }
}
class A {
constructor(x, y) { this.x = x; this.y = y }
}
VM602:1 Uncaught SyntaxError: Identifier 'A' has already been declared(…)
# Two
class A {
constructor(x) { this.x = x }
}
delete A
true
class A {
constructor(x) { this.x = x }
}
VM805:1 Uncaught SyntaxError: Identifier 'A' has already been declared(…)
# Three
A = null
null
class A …Run Code Online (Sandbox Code Playgroud)