Easiest to explain with code:
最容易用代码解释:
##### module.js
var count, incCount, setCount, showCount;
count = 0;
showCount = function() {
return console.log(count);
};
incCount = function() {
return count++;
};
setCount = function(c) {
return count = c;
};
exports.showCount = showCount;
exports.incCount = incCount;
exports.setCount = setCount;
exports.count = count; // let's also export the count variable itself
#### test.js
var m;
m = require("./module.js");
m.setCount(10);
m.showCount(); // outputs 10
m.incCount();
m.showCount(); // outputs 11
console.log(m.count); // outputs 0
The exported functions work as expected. But I'm not clear why m.count isn't also 11.
导出的函数按预期工作。但我不清楚为什么m.count也不是11。
3 个解决方案
#1
13
exports.count = count
exports.count = count
Your setting a property count
on an object exports
to be the value of count
. I.e. 0.
您在对象上设置属性计数导出为count的值。即0。
Everything is pass by value not pass by reference.
一切都是通过价值而不是通过参考传递。
If you were to define count
as a getter like such :
如果你将count定义为这样的getter:
Object.defineProperty(exports, "count", {
get: function() { return count; }
});
Then exports.count
would always return the current value of count
and thus be 11
那么exports.count将始终返回count的当前值,因此为11
#2
0
Correct me if I am wrong, but numbers are immutable types. When you change the value of count
then your reference changes too. So exports.count
references to the old count
value.
如果我错了,请纠正我,但数字是不可变的类型。当您更改count的值时,您的引用也会更改。所以exports.count引用旧计数值。
#3
0
In JavaScript, functions and objects (including arrays) are assigned to variables by reference, and strings and numbers are assigned by value--that is, by making a copy. If var a = 1
and var b = a
and b++
, a
will still equal 1.
在JavaScript中,函数和对象(包括数组)通过引用分配给变量,字符串和数字按值分配 - 即通过制作副本。如果var a = 1且var b = a和b ++,则a仍将等于1。
On this line:
在这一行:
exports.count = count; // let's also export the count variable itself
you made a by-value copy of the count variable. The setCount(), incCount() and showCount() operations all operate on the count variable inside the closure, so m.count doesn't get touched again. If those variables were operating on this.count, then you'd get the behavior you expect--but you probably don't want to export the count variable anyway.
你制作了count变量的副值副本。 setCount(),incCount()和showCount()操作都对闭包内的count变量进行操作,因此m.count不再被触及。如果这些变量在this.count上运行,那么你会得到你期望的行为 - 但你可能不想导出计数变量。
#1
13
exports.count = count
exports.count = count
Your setting a property count
on an object exports
to be the value of count
. I.e. 0.
您在对象上设置属性计数导出为count的值。即0。
Everything is pass by value not pass by reference.
一切都是通过价值而不是通过参考传递。
If you were to define count
as a getter like such :
如果你将count定义为这样的getter:
Object.defineProperty(exports, "count", {
get: function() { return count; }
});
Then exports.count
would always return the current value of count
and thus be 11
那么exports.count将始终返回count的当前值,因此为11
#2
0
Correct me if I am wrong, but numbers are immutable types. When you change the value of count
then your reference changes too. So exports.count
references to the old count
value.
如果我错了,请纠正我,但数字是不可变的类型。当您更改count的值时,您的引用也会更改。所以exports.count引用旧计数值。
#3
0
In JavaScript, functions and objects (including arrays) are assigned to variables by reference, and strings and numbers are assigned by value--that is, by making a copy. If var a = 1
and var b = a
and b++
, a
will still equal 1.
在JavaScript中,函数和对象(包括数组)通过引用分配给变量,字符串和数字按值分配 - 即通过制作副本。如果var a = 1且var b = a和b ++,则a仍将等于1。
On this line:
在这一行:
exports.count = count; // let's also export the count variable itself
you made a by-value copy of the count variable. The setCount(), incCount() and showCount() operations all operate on the count variable inside the closure, so m.count doesn't get touched again. If those variables were operating on this.count, then you'd get the behavior you expect--but you probably don't want to export the count variable anyway.
你制作了count变量的副值副本。 setCount(),incCount()和showCount()操作都对闭包内的count变量进行操作,因此m.count不再被触及。如果这些变量在this.count上运行,那么你会得到你期望的行为 - 但你可能不想导出计数变量。