“Método de enlace en JS” Código de respuesta

Back en JavaScript

bind() returns a bound function that, when executed later, will have the correct context ("this") for calling the original function.
Confused Curlew

¿Cuál es el uso de Bind () en JavaScript?

var myButton = {
  content: 'OK',
  click() {
    console.log(this.content + ' clicked');
  }
};

myButton.click();

var looseClick = myButton.click;
looseClick(); // not bound, 'this' is not myButton - it is the globalThis

var boundClick = myButton.click.bind(myButton);
boundClick(); // bound, 'this' is myButton
Rashad Almaqtary

Método de enlace en JS

ar bikeDetails = {
    displayDetails: function(registrationNumber,brandName){
    return this.name+ " , "+ "bike details: "+ registrationNumber + " , " + brandName;
  }
}
        
var person1 = {name:  "Vivek"};
        
var detailsOfPerson1 = bikeDetails.displayDetails.bind(person1, "TS0122", "Bullet");
        
// Binds the displayDetails function to the person1 object
        
        
detailsOfPerson1();
// Returns Vivek, bike details: TS0452, Thunderbird
pranav preethi

Método de enlace en JS

ar bikeDetails = {
    displayDetails: function(registrationNumber,brandName){
    return this.name+ " , "+ "bike details: "+ registrationNumber + " , " + brandName;
  }
}
        
var person1 = {name:  "Vivek"};
        
var detailsOfPerson1 = bikeDetails.displayDetails.bind(person1, "TS0122", "Bullet");
        
// Binds the displayDetails function to the person1 object
        
        
detailsOfPerson1();
// Returns Vivek, bike details: TS0452, Thunderbird
pranav preethi

Respuestas similares a “Método de enlace en JS”

Preguntas similares a “Método de enlace en JS”

Más respuestas relacionadas con “Método de enlace en JS” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código