Welcome Guest! (App Version 3.1)

websitnero logo name

Call, Apply, Bind

Lets see a situation where there is function which has got some code inside it. Some of its code has got 'this' keyword. Now, here comes the catch. We want to use this function for an other object (say Obj1), but the problem is that this function is not a property of that object (but rather a generalized function). So, now how to use this function in context of Obj1 so that 'this' inside the code of function attaches itself to our beloved object (obj1) and not window object (which is the context by default).

There are 3 options to achieve this task, which are:

  1. Call
  2. Apply
  3. Bind

Call

Till the last version of HTML data could only be stored at client side in cookies which had a limit of 4KB. Now, webstorage can also be used to store data on the client side.

Example

<script>

var person1 = {firstName: 'Peter', age: 29};
var person2 = {firstName: 'Felicity', age: 23};

var alpha = function(){
  alert(this.firstName + "  " + this.age);
};

alpha(); //this points here to context of window object
alpha.call(person1); //attaching context to custom object via call
alpha.call(person2); //attaching context to custom object via call 

</script>

Apply

Till the last version of HTML data could only be stored at client side in cookies which had a limit of 4KB. Now, webstorage can also be used to store data on the client side.

Example

<script>

var person1 = {firstName: 'Peter', age: 29};
var person2 = {firstName: 'Felicity', age: 23};

var alpha = function(){
  alert(this.firstName + "  " + this.age);
};

alpha(); //this points here to context of window object
alpha.apply(person1); //attaching context to custom object via apply
alpha.apply(person2); //attaching context to custom object via apply 

</script>

Bind

Till the last version of HTML data could only be stored at client side in cookies which had a limit of 4KB. Now, webstorage can also be used to store data on the client side.

Example

<script>

var person1 = {firstName: 'Peter', age: 29};
var person2 = {firstName: 'Felicity', age: 23};

var alpha = function(){
  alert(this.firstName + "  " + this.age);
};

alpha(); //this points here to context of window object
delta = alpha.bind(person1); //attaching context and keeping for future use
eppa = alpha.bind(person2); //attaching context and keeping for future use 
delta();
eppa();

</script>
WebsiteNero is highly optimized for learning various website developing technlogies. We try our best to add maximum modules and examples to help learn the concepts clearly and vividly. We try to present all content and examples as simple as we can removing any complexity to hurdle easy understanding. We try best provide you with worthful content, but we cannot guarantee full correctness of all the content on this site. While using this website, you agree to have read and accepted all our terms of use and conditions, cookie, and privacy policy. Copyright, 2013-2018 by websitenero.com. All Rights Reserved.