Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

what is the use of bind method in javascript

"Variables has local and global scopes. Let's suppose that we have two variables
with the same name. One is globally defined and the other is defined inside a
function closure and we want to get the variable value which is inside the
function closure. In that case we use this bind() method.

code:

var x = 9; // this refers to global "window" object here in the browser

var person = {
  x: 81, // local variable x = 81
  getX: function() {
    return this.x; // "this" = person , so this.x is the same as  person.x
  }
};
//  the next line of code is the most important one (line 17)
var y = person.getX; // It will return 9, because it will call global value of x(var x=9).

var myFunc = y.bind(person); // It will return 81, because it will call local value of x, which is defined in the object called person(x=81).

y(); // return 9 (the global variable);

myFunc();" // return 81 (the loacl variable);
Comment

bind in javascript

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

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. 
Comment

JavaScript Function bind()


		const person = {
name: "John Smith",
getNameAndAddress: function()
{
return this.name;
}

}
const personWithoutGet = {
name: "Jerry Smithers"}

 console.log(person.getName.bind(personWithoutGet);
  }

/*you can think of bind() apply()  and call() ALL as "stealing someone else's function to use."*/
/* victimBeingStolenFrom.functionName.call(theThief) */
/*notice the second variable personWithoutGet does not have a getName function*/
     
Comment

What is the use of bind() in 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
Comment

JavaScript Function 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);
Comment

bind() method

var car = {
    registrationNumber: "FT5142",
    brand: "Benz",

    displayDetails: function(){
        console.log(this.registrationNumber+ " " + this.brand );
    }
}
car.displayDetails();


var myCarDetails = car.displayDetails.bind(car);
myCarDetails();
Comment

bind in javascript example

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'
Comment

Bind() In 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*/
Comment

bind method in 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
Comment

bind method in 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
Comment

PREVIOUS NEXT
Code Example
Javascript :: usestate callback 
Javascript :: state in react 
Javascript :: javascript execute function after async 
Javascript :: xml vs json 
Javascript :: e.target.value to image url in javascript 
Javascript :: js contenteditable button spacebar 
Javascript :: how can i do metaname csrf token attrcontent in vanilla javascrip 
Javascript :: toggle button in angularjs bootstrap 
Javascript :: spray scala JSON formatter override def read examples 
Javascript :: math floor html 
Javascript :: ways of defining object js 
Javascript :: firebase get key value 
Javascript :: defining functions in react 
Javascript :: remove equal json js 
Javascript :: react router dom default params 
Javascript :: for of loop ecmascript 6 
Javascript :: recaptcha v3 
Javascript :: vuejs copy to clipboard 
Javascript :: hot to set file views in nodejs 
Javascript :: regex validate wallet eth 
Javascript :: how to read with attr in jquery 
Javascript :: mongoose search multiple fields 
Javascript :: acer swift 5 
Javascript :: How to validate an unicode email address in JavaScript 
Javascript :: ajaxstart not working in chrome 
Javascript :: react import coreui icons 
Javascript :: create a drop down to select time javascript 
Javascript :: leaflet core 
Javascript :: Pass unknown number of arguments into javascript function 
Javascript :: Getting Search Parameters From A URL As An Array 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =