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

99
Views
¿En qué casos queremos usar apply con esto?

Entiendo cómo funciona esto, pero no puedo entender por qué queremos usar apply con la palabra clave "this" como en el ejemplo a continuación:

 function curry(func) { return function curried(...args) { if (args.length >= func.length) { return func.apply(this, args) } else { return curried.bind(this, ...args) } } }

Aquí vincular y aplicar usa "esto" como primer argumento, pero ¿cuál es el propósito si solo podemos hacer func (args) ya que esto apunta al mismo entorno léxico de una función? Puedo ver algunos beneficios con funciones de flecha, pero aquí he nombrado funciones. ¿No hay diferencia o me estoy perdiendo algo?

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

0

La razón para usar apply es mantener el mismo valor de this . Llamar a func(args) daría como resultado que this sea el objeto de la window (en modo no estricto) o undefined (en modo estricto).

Aquí hay un ejemplo que se rompe si llamas a func(args) :

 function curry(func) { return function curried(...args) { if (args.length >= func.length) { return func(args); } else { return curried.bind(this, ...args); } }; } const example = { multiplier: 5, calculate: function (a, b) { return (a + b) * this.multiplier; }, }; example.curriedVersion = curry(example.calculate); // Because of the way these are being called, `this` should be `example` console.log(example.calculate(1, 2)); // But here it's not, causing unexpected results console.log(example.curriedVersion(1)(2));

Pero funciona si apply(this, args) :

 function curry(func) { return function curried(...args) { if (args.length >= func.length) { return func.apply(this, args); } else { return curried.bind(this, ...args); } }; } const example = { multiplier: 5, calculate: function (a, b) { return (a + b) * this.multiplier; }, }; example.curriedVersion = curry(example.calculate); // Because of the way these are being called, `this` should be `example` console.log(example.calculate(1, 2)); console.log(example.curriedVersion(1)(2));

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!