Javascript 这里是: let a = {}; function foo() { a = 'test'; }; function getA() { return a; // this works because we've created a closure } module.exports.foo = foo; module.exports.getA = getA;
现在您可以执行以下操作:Javascript 这里是: let a = {}; function foo() { a = 'test'; }; function getA() { return a; // this works because we've created a closure } module.exports.foo = foo; module.exports.getA = getA;,javascript,node.js,scope,Javascript,Node.js,Scope,现在您可以执行以下操作: a.foo(); a.getA(); // should return 'test' 您已将a定义为对象。对象包含键值对。因此,在第一个示例中尝试编写a={b:'test'},您已经将a定义为一个对象。对象包含键值对。因此,在第一个示例中尝试编写a={b:'test'}。 { foo: [Function: foo], a: {} } { foo: [Fuction: foo], a: 'test' } let a = {}; function foo() {
a.foo();
a.getA(); // should return 'test'
您已将
a
定义为对象。对象包含键值对。因此,在第一个示例中尝试编写a={b:'test'}
,您已经将a
定义为一个对象。对象包含键值对。因此,在第一个示例中尝试编写a={b:'test'}
。
{ foo: [Function: foo], a: {} }
{ foo: [Fuction: foo], a: 'test' }
let a = {};
function foo() {
a.b = 'test';
};
module.exports.foo = foo;
module.exports.a = a;
{ foo: [Function: foo], a: { b: 'test' } }
function callerDoesNotSeeThis(a){ a = 1 }
function callerSeesThis(a){ a.foo = 1 }
let a = {};
function foo() {
a = 'test';
};
module.exports.foo = foo;
module.exports.a = a;
a.foo();
function foo() {
this.a = 'test';
};
module.exports.foo = foo;
module.exports.a = {};
let a = {};
function foo() {
a = 'test';
};
function getA() {
return a; // this works because we've created a closure
}
module.exports.foo = foo;
module.exports.getA = getA;
a.foo();
a.getA(); // should return 'test'