Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda