Solved

Having trouble understanding javascript bind

Posted on 2013-12-10
6
241 Views
Last Modified: 2013-12-11
I took a look at the following url and explanation but still don't get it


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

Can somebody explain it better to me
0
Comment
Question by:stargateatlantis
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 39709366
When you call a function as a method of an object, the context of the method is the object, and you can access the object using the this keyword.

Example:

var obj = {
  info: 'asdf',
  fn: function(){
    alert(this.info);
  }
};

obj.fn(); // alerts "asdf"

Open in new window


The bind method allows you to create a function reference that will act as if you called it as a method in an object, without the function actually being part of the object.

Example:

var obj = {
  info: 'asdf'
};

function showInfo() {
  alert(this.info);
}

var f = showInfo.bind(obj);
f(); // alerts "asdf"

Open in new window

0
 

Author Comment

by:stargateatlantis
ID: 39710207
Can you bind a object that contains a function??
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 39710430
Yes, you can use any object as context for the function.

You could even use something like "a string" or 42 as context, then the context would be a String object or a Number object, respectively.

If you would use null or undefined as context, the function would get the window object as context.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:stargateatlantis
ID: 39710434
is there a more complex example
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 39711480
Here is an example with an object constructor and binding a function to an instance of the object:

function Employee(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

Employee.prototype = {

  showName: function(){
    alert(this.firstName + ' ' + this.lastName);
  }

};

// create an emplyee and show the name
var john = new Employee('John', 'Doe');
john.showName();

//bind a function expression to the employee
var showNameReverse = (function(){
  alert(this.lastName + ', ' + this.firstName);
}).bind(john);

// use it
showNameReverse();

Open in new window

0
 

Author Closing Comment

by:stargateatlantis
ID: 39712750
Thanks for the help that is what I was looking for
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now