Bind() method in javascript

 Bind is a function that helps you create another function that you can execute later with the new context of this that is provided.


The bind function creates a copy of a function with a new value to the this present inside the calling function.

Here is the syntax for the bind function:

func.bind(thisObj, arg1, arg2, ..., argN);

Where,

  • func is a function that needs to be invoked with a different this object
  • thisObj is an object or a value that needs to be replaced with the this keyword present inside the function func
  • arg1, arg2…argN – you can pass 1 argument to the calling function or more than that, similar to the call function.

The bind function then returns a new function that consists of a new context to the this variable present inside the calling function:

func(arg1, arg2);

Now this function func can be executed later with the arguments.

Example:-

var pokemon = {

    firstname: 'Pika',

    lastname: 'Chu ',

    getPokeName: function() {

        var fullname = this.firstname + ' ' + this.lastname;

      console.log(fullname) ;

    }

};

var output=pokemon.getPokeName;

output(); //undefined undefined

// undefined because in a recent javascript version the window object has property called name which has a value equal to empty string ""


// we want this point to pokeman object not to window object 

//we can apply bind method to point object pokemon inplace of window

var output=pokemon.getPokeName.bind(pokemon);

output(); // Pika Chu 

example

const person = {

  firstName:"John",

  lastName: "Doe",

  fullName: function () {

console.log(this.firstName + " " + this.lastName);

  }

}


const member = {

  firstName:"Hege",

  lastName: "Nilsen",

}

person.fullName.call(member);//Hege Nilsen


var output=person.fullName;

output(); //undefined undefined

// i want  it point to person object in place of global object

var output2=person.fullName.bind(person);

output2();//John Doe


let output3=person.fullName.bind(member);

output3();//Hege Nilsen


example

var person={

    name:"john",

    ask:function()

    {

        console.log(this);

    }

}

new(person.ask.bind(person)) ();  // ask {}

// it point to person object

// bind create a new function var person={

    name:"john",

    ask:function()

    {

        console.log(this);

    }

}

new(person.ask.bind(person)) ();  // ask {}

// it point to person object 

// new object used to crteate object var person={

    name:"john",

    ask:function()

    {

        console.log(this);

    }

}

new(person.ask.bind(person)) ();  // ask {}

// it point to person object 

// new object used to crteate object

// bind create a new function

// after that you call it 

// then you get output ,point to person object

// bind create a new function ; using the new ‘keyword’.






explain through Html ,Css,Javascript


<!DOCTYPE html>

<html>

<body>


<h1>JavaScript Function bind()</h1>


<p>This example creates 2 objects (person and member).</p>

<p>The member object borrows the fullname method from person:</p> 


<p id="demo"></p>


<script>

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);


document.getElementById("demo").innerHTML = fullName();

</script>


</body>

</html>

//output 

//This example creates 2 objects (person and member).

The member object borrows the fullname method from person:

Hege Nilsen


Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence