Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
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 - Fatal编程技术网

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'