“Bind () en javascript” Réponses codées

Bind Method in 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

se lier à JavaScript

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

À quoi sert Bind () dans 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

Fonction javascript bind ()

const person = {
  firstName:"John",
  lastName: "Doe",
  fullName: function () {
    return this.firstName + " " + this.lastName;
  }
}

const member = {
  firstName:"Hege",
  lastName: "Nilsen",
}

let fullName = person.fullName.bind(member);
naly moslih

lier l'exemple 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

Bind () en javascript

		  (function()
	  {
		
		const student =
		  {
			  name :'John',
			  getName : function()
			  {
				  return this.name;
			  }
		  }
		  
	let theName =student.getName.bind(student);
	console.log(theName());

	  })()
      /*use bind when you have a function(not one created from a class) that requires a this, and you add the this candidate inside of bind*/
Javasper

Réponses similaires à “Bind () en javascript”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code