Solved

Having trouble understanding javascript bind

Posted on 2013-12-10
6
232 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now