“Vincular no exemplo JavaScript” Respostas de código

Método de ligação em JavaScript


let obj = {
    name: "Abhi",
    age: 28,
    info: function() {
        console.log(`${this.name}  is  ${this.age} year's old`);//template literals used here
    },
};
let obj1 = {
    name: "Vikash",
    age: 28,
};

obj.info.bind(obj1)(); //since bind methods return a new function so 
//we can directly call that function[()] without pollouting global
//space while assigning a varibale to call that function.
//Always try to make global space neat n clean ,
//dont polloute while assigning unnecessary variables. 
Brave Soul

vincular em JavaScript

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

Qual é o uso de bind () em 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

Vincular no exemplo JavaScript

var persona = {
  nombre: 'Franco',
  apellido: 'Chequer',

  getNombre: function(){
    var nombreCompleto = this.nombre + ' ' + this.apellido;
    return nombreCompleto;
  }
}

var logNombre = function(){
  console.log(this.getNombre());
}

var logNombrePersona = logNombre.bind(persona);

logNombrePersona(); //'Franco'
Sleepy Scarab

Método de ligação em 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

Respostas semelhantes a “Vincular no exemplo JavaScript”

Perguntas semelhantes a “Vincular no exemplo JavaScript”

Mais respostas relacionadas para “Vincular no exemplo JavaScript” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código