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

168
Views
Comportamiento de la función de llamada simple de JavaScript

Quiero preguntar sobre el comportamiento de la función de llamada. Me quedo atascado cada vez que trato de entender la función de call .

¿Alguien puede ayudarme a entender lo que está pasando sugiriendo la implementación del método de find ?

 Hoge = function (val) { this.val = val; //console.log("this.val" + this.val); }; Hoge.prototype.find = function (callback) { callback.call(this.val); }; var h = new Hoge(1); h.find((o) => { console.log(o); // expected 1 but undefined console.log(o === 1); // expected true but its false (caz o is undefined) });
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

El primer parámetro de Function#call es this valor. Para invocar directamente una función, solo use paréntesis.

 callback(this.val); 

 Hoge = function (val) { this.val = val; }; Hoge.prototype.find = function (callback) { callback(this.val); }; var h = new Hoge(1); h.find((o) => { console.log(o); console.log(o === 1); });

about 4 years ago · Juan Pablo Isaza Report

0

El primer parámetro en la call será el nuevo this desea usar. Luego, en adelante están los parámetros que desea pasar.

Básicamente, no estás haciendo mucho uso de .call aquí, y si simplemente quieres usar o puedes pasar null y this.val .

Aún más simple, simplemente invoque la función de callback de llamada

 Hoge = function (val) { this.val = val; //console.log("this.val" + this.val); }; Hoge.prototype.find = function (callback) { //Both work callback.call(null,this.val); callback(this.val); }; var h = new Hoge(1); h.find((o) => { console.log(o); // expected 1 but undefined console.log(o === 1); // expected true but its false (caz o is undefined) });

about 4 years ago · Juan Pablo Isaza Report

0

No está pasando call a un argumento thisArg . Bueno, lo eres , pero es el valor que deseas pasar a la devolución de llamada.

 Hoge = function(val) { this.val = val; //console.log("this.val" + this.val); }; Hoge.prototype.find = function(callback) { // callback.call(this.val); callback.call(this, this.val); // call wants a "this" argument // callback(this.val); // Could just do this instead }; var h = new Hoge(1); h.find((o) => { console.log(o); // expected 1 but undefined console.log(o === 1); // expected true but its false (caz o is undefined) });

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!