考虑以下代码片段:
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
我认为在构造函数之外无法访问私有变量?