【发布时间】:2013-08-03 08:14:02
【问题描述】:
考虑以下代码sn-p:
function C1() {
// private variable in the constructor
a = 1;
}
C1.prototype.f1 = function() {
console.log( "a=" + a );
}
C1.prototype.f2 = function() {
a = 2;
process.nextTick( this.f1 );
}
o = new C1();
o.f1();
o.f2();
观察到的输出是:
a=1
a=2
我认为私有变量不能在构造函数之外访问?
【问题讨论】:
-
你必须declare the variable --
var a = 1;。简单地设置它可以创建一个全局。 stackoverflow.com/q/1470488
标签: javascript node.js asynchronous private member