简体   繁体   中英

Is it possible to access a variable from the caller's scope inside a function in JavaScript?

This question has been asked before, but all of the popular questions are 5+ years old and I'm curious to know if anything has changed since then. If you have a function that's defined somehwere

const accessParentScope = () => parentVariable;

then is there any way to access a parentVariable from the scope where the function is called? The end goal would be to do something like

function createAnotherScope() {
  const parentVariable = 'some value';
  return accessParentScope();
}

and have accessParentScope() have access to parentVariable without explicitly passing it as an argument.

Alternatively, is it possible to access variables from the scope of a closure? If you have a function like

function createClosure() {
  const parentVariable = 'some value';
  return closure = () => null;
}

then can you do something like createClosure().parentVariable ? The syntax here obviously won't work, but I'm curious if anything remotely like this is possible.

Is it possible to access a variable from the caller's scope inside a function in JavaScript?

No. That would be dynamic scope . Most languages (including JavaScript) implement lexical scope . That is not going to change.

There is this , but it's rather an explicitly passed argument. The value of this is (in most cases) determined when the function is called , not when or where it is defined (arrow functions treat this differently though).

 function logName() { console.log(this.name); } function sayFoo() { logName.call({name: 'foo'}); } sayFoo(); function sayBar() { logName.call({name: 'bar'}); } sayBar(); 

As you can see, there really isn't any advantage of this over defining the function with parameters:

 function logName(name) { console.log(name); } function sayFoo() { logName('foo'); } sayFoo(); function sayBar() { logName('bar'); } sayBar(); 

As @JaromandaX said in their comment, that's what parameters are therefore, to provide values to the function at call time.

is there any way to access a parentVariable from the scope where the function is called?

No. The only way is if the context where the arrow function was declared has that attribute or variable available.

 var parentVariable = 'Ele from SO'; // This is the variable available to the below arrow function (window context). const accessParentScope = () => parentVariable; // or this.parentVariable function createAnotherScope() { const parentVariable = 'some value'; return accessParentScope(); } console.log(createAnotherScope()) 

Alternatively, is it possible to access variables from the scope of a closure?

Yes, that way you can access the declared attributes and local variables.

 function createClosure() { this.parentVariable = 'some value'; // Note that this is an attribute (global as well) rather than a local variable. return closure = () => this.parentVariable; } console.log(createClosure()()); console.log(parentVariable); // Access to global variable/attribute 

Then can you do something like createClosure().parentVariable?

No, what you can do is to set an attribute to the returned function.

 function createClosure() { var closure = () => closure.parentVariable closure.parentVariable = 'some value'; return closure; } console.log(createClosure()()); console.log(createClosure().parentVariable) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM