JavaScript。为什么在创建新对象时会覆盖私有函数?
JavaScript. Why private function is overrided when creating new object?
您好,我对对象和 setInterval 有疑问。看起来变量在所有实例之间共享。这是示例代码:
function intervals(objectName) {
var interval;
var name = objectName;
this.makeInterval = function() {
clearInterval(interval);
interval = setInterval(function() { console.log('hello world'); }, 20000);
};
this.intervalIdToConsole = function() {
console.log(name + ' interval ID: ' + interval);
};
this.stop = function() {
stopInterval();
};
stopInterval = function() {
console.log('Stopping ' + name + ' id: ' + interval);
clearInterval(interval);
};
}
var int1 = new intervals('object1');
var int2 = new intervals('object2');
int1.makeInterval();
int1.intervalIdToConsole();
int2.makeInterval();
int2.intervalIdToConsole();
console.log('-----------------');
int1.intervalIdToConsole();
int2.intervalIdToConsole();
console.log('-----------------');
int1.stop();
int2.stop();
当我 运行 在 Web 浏览器中输出此代码时,我收到如下消息:
object1 interval ID: 5
object2 interval ID: 6
-----------------
object1 interval ID: 5
object2 interval ID: 6
-----------------
Stopping object2 id: 6
Stopping object2 id: 6
这是我的问题。我做错了什么?为什么在 int1.stop() 上调用 object2 中的 stopInterval()?
编辑
哦,天哪,它现在可以工作了。谢谢!为了更好的理解。如果我在没有 var 语句的情况下声明变量那么它是全局变量?
你只是忘记了 var
。此代码有效:
function intervals(objectName) {
var interval;
var name = objectName;
this.makeInterval = function() {
clearInterval(interval);
interval = setInterval(function() { console.log('hello world'); }, 20000);
};
this.intervalIdToConsole = function() {
console.log(name + ' interval ID: ' + interval);
};
this.stop = function() {
stopInterval();
};
var stopInterval = function() {
console.log('Stopping ' + name + ' id: ' + interval);
clearInterval(interval);
};
}
var int1 = new intervals('object1');
var int2 = new intervals('object2');
int1.makeInterval();
int1.intervalIdToConsole();
int2.makeInterval();
int2.intervalIdToConsole();
console.log('-----------------');
int1.intervalIdToConsole();
int2.intervalIdToConsole();
console.log('-----------------');
int1.stop();
int2.stop();
我在stopInterval
之前添加了var
。
输出:
object1 interval ID: 1
object2 interval ID: 2
-----------------
object1 interval ID: 1
object2 interval ID: 2
-----------------
Stopping object1 id: 1
Stopping object2 id: 2
因为stopInterval
是一个全局变量,因此被下一次赋值覆盖。
为了防止这种情况,使用 var
关键字在 intervals
函数范围内声明变量,或者使用 this.stopInterval = ...
.
作为该函数的成员
提示:使用严格模式防止意外声明全局变量:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
您好,我对对象和 setInterval 有疑问。看起来变量在所有实例之间共享。这是示例代码:
function intervals(objectName) {
var interval;
var name = objectName;
this.makeInterval = function() {
clearInterval(interval);
interval = setInterval(function() { console.log('hello world'); }, 20000);
};
this.intervalIdToConsole = function() {
console.log(name + ' interval ID: ' + interval);
};
this.stop = function() {
stopInterval();
};
stopInterval = function() {
console.log('Stopping ' + name + ' id: ' + interval);
clearInterval(interval);
};
}
var int1 = new intervals('object1');
var int2 = new intervals('object2');
int1.makeInterval();
int1.intervalIdToConsole();
int2.makeInterval();
int2.intervalIdToConsole();
console.log('-----------------');
int1.intervalIdToConsole();
int2.intervalIdToConsole();
console.log('-----------------');
int1.stop();
int2.stop();
当我 运行 在 Web 浏览器中输出此代码时,我收到如下消息:
object1 interval ID: 5
object2 interval ID: 6
-----------------
object1 interval ID: 5
object2 interval ID: 6
-----------------
Stopping object2 id: 6
Stopping object2 id: 6
这是我的问题。我做错了什么?为什么在 int1.stop() 上调用 object2 中的 stopInterval()?
编辑 哦,天哪,它现在可以工作了。谢谢!为了更好的理解。如果我在没有 var 语句的情况下声明变量那么它是全局变量?
你只是忘记了 var
。此代码有效:
function intervals(objectName) {
var interval;
var name = objectName;
this.makeInterval = function() {
clearInterval(interval);
interval = setInterval(function() { console.log('hello world'); }, 20000);
};
this.intervalIdToConsole = function() {
console.log(name + ' interval ID: ' + interval);
};
this.stop = function() {
stopInterval();
};
var stopInterval = function() {
console.log('Stopping ' + name + ' id: ' + interval);
clearInterval(interval);
};
}
var int1 = new intervals('object1');
var int2 = new intervals('object2');
int1.makeInterval();
int1.intervalIdToConsole();
int2.makeInterval();
int2.intervalIdToConsole();
console.log('-----------------');
int1.intervalIdToConsole();
int2.intervalIdToConsole();
console.log('-----------------');
int1.stop();
int2.stop();
我在stopInterval
之前添加了var
。
输出:
object1 interval ID: 1
object2 interval ID: 2
-----------------
object1 interval ID: 1
object2 interval ID: 2
-----------------
Stopping object1 id: 1
Stopping object2 id: 2
因为stopInterval
是一个全局变量,因此被下一次赋值覆盖。
为了防止这种情况,使用 var
关键字在 intervals
函数范围内声明变量,或者使用 this.stopInterval = ...
.
提示:使用严格模式防止意外声明全局变量:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode