Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Execute function on different scope: Javascript

So I think it's just not possible, yet I wonder... is there a way to create a function that works like this?

const someFunction=()=> console.log(a); //printing some ref that is not out of this scope;

const wrappedFn = wrapFnOnScope(someFunction,{a:"Hello World!"});

wrappedFn() // prints Hello World!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is indeed impossible. JavaScript has lexical scope, which means the scope of the variables in your closure depends (only) on where you did define the function. It is not possible to change this afterwards.

If you want late binding, you need to use parameters in JavaScript. It might be

const someFunction = (obj) => console.log(obj.a);
someFunction({a: 'Hello World'});
// or
const wrappedFunction = () => someFunction({a: 'Hello World'});
const wrappedFunction = someFunction.bind(undefined, {a: 'Hello World'});

or using the implicit zeroth parameter this:

function someFunction() { console.log(this.a); }
someFunction.call({a: 'Hello World'});
// or
const wrappedFunction = () => someFunction.call({a: 'Hello World'});
const wrappedFunction = someFunction.bind({a: 'Hello World'});

If you insist on referring to an a variable in the someFunction, use destructuring

const someFunction = ({a}) => console.log(a);
function someFunction() { const {a} = this; console.log(a); }

or even the dreaded with statement (bad idea!), but ultimately you must do this inside the someFunction. There is no magic wrapFnOnScope.

about 4 years ago · Juan Pablo Isaza Report

0

You appear to be conflating variables and properties. A variable is a temporary name given to a value in the code. A property is a key-value pair stored inside an object.

If you want to create a function which simply extracts a certain property from an object, you can just do it like so:

const bird = { wings: 2, color: "red" };
const getColor = value => value.color;
const birdColor = getColor(bird); // "red"

Secondly, you are trying to assign arguments in advance to a function, but only to execute said function later. This can be done by wrapping the function call in an a function without arguments like so:

const a = 1;
const b = 2;
const c = 3;

const delayedApplication = () => console.log(a + b + c);
// nothing happened yet
delayedApplication();
// "6" has been logged to the console
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!