Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 11th Sep 2015, 09:14 PM   #1
Warrior Member
 
Join Date: 2015
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
Default
what is bind() method in javascript?
Share on: 
fb share twitter share gplus share more share

what is bind() method in javascript?
protekconsulting is offline  
Unread 12th Sep 2015, 11:55 AM   #2
Active Warrior
 
ynef's Avatar
 
Join Date: 2015
Location: Tallinn, Estonia
Posts: 91
Thanks: 2
Thanked 20 Times in 18 Posts
Default
Re: what is bind() method in javascript?
Share on: 
fb share twitter share gplus share more share

bind()
is used to bind the value of
this
to an object. In other words you can choose specifically which object will be bound to
this
so you can use it within your code.

Content writer, web developer, SEO consultant - Ynef's Portfolio
ynef is offline  
Unread 12th Sep 2015, 12:54 PM   #3
Webmaster
 
webcosmo's Avatar
 
Join Date: 2008
Location: Boston, MA
Posts: 531
Thanks: 6
Thanked 65 Times in 58 Posts
Default
Re: what is bind() method in javascript?
Share on: 
fb share twitter share gplus share more share

It will bound ' this' value to the first argument of bind function. If you bind some object to another, this.var will become var of the binded object.

webcosmo is offline  
Unread 14th Sep 2015, 04:30 AM   #4
Web Developer
 
Join Date: 2015
Location: Bangladesh
Posts: 37
Thanks: 0
Thanked 6 Times in 6 Posts
Default
Re: what is bind() method in javascript?
Share on: 
fb share twitter share gplus share more share

The bind() method attaches one or more event handlers for selected elements, and specifies a function to run when the event occurs.
eg.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").bind("click", function(){
alert("The paragraph was clicked.");
});
});
</script>
</head>
<body>

<p>Click me!</p>

</body>
</html>

I can fix website issues only $5
Sowkat Hossain is offline  
Unread 14th Sep 2015, 11:59 AM   #5
HyperActive Warrior
 
Join Date: 2014
Location: bangalore
Posts: 113
Thanks: 0
Thanked 11 Times in 11 Posts
Default
Re: what is bind() method in javascript?
Share on: 
fb share twitter share gplus share more share

bind value base on tha parameters, its like a function

sysntex of bind method

fun.bind(thisArg[, arg1[, arg2[, ...]]])

where arg1 , arg 2 is a variable you put a values of those variables

yasar is offline  
Unread 15th Sep 2015, 03:48 AM   #6
Warrior Member
 
Join Date: 2015
Posts: 7
Thanks: 0
Thanked 2 Times in 2 Posts
Default
Re: what is bind() method in javascript?
Share on: 
fb share twitter share gplus share more share

Bind creates a new function that will have this set to the first parameter passed to bind().


Here's an example that shows how to use bind to pass a member method around that has the correct this:
var Button = function(content) { this.content = content; }; Button.prototype.click = function() { console.log(this.content + ' clicked'); } var myButton = new Button('OK'); myButton.click(); var looseClick = myButton.click; looseClick(); // not bound, 'this' is not myButton var boundClick = myButton.click.bind(myButton); boundClick(); // bound, 'this' is myButton Which prints out:

OK clicked undefined clicked OK clicked You can also add extra parameters after the 1st parameter and bind will pass in those values to the original function before passing in the extra parameters you pass to the bound function:
// Example showing binding some parameters var sum = function(a, b) { return a + b; }; var add5 = sum.bind(null, 5); console.log(add5(10)); Which prints out:
Nimmy Wilson is offline  
Closed Thread


Bookmarks

Tags
bind, javascript, method


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 10:39 PM.