Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda