Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Website Design (https://www.warriorforum.com/website-design/)
-   -   what is bind() method in javascript? (https://www.warriorforum.com/website-design/1109976-what-bind-method-javascript.html)

protekconsulting 11th September 2015 09:14 PM

what is bind() method in javascript?
 
what is bind() method in javascript?

ynef 12th September 2015 11:55 AM

Re: what is bind() method in javascript?
 
Quote:

bind()
is used to bind the value of
Quote:

this
to an object. In other words you can choose specifically which object will be bound to
Quote:

this
so you can use it within your code.

webcosmo 12th September 2015 12:54 PM

Re: what is bind() method in javascript?
 
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.

Sowkat Hossain 14th September 2015 04:30 AM

Re: what is bind() method in javascript?
 
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>

yasar 14th September 2015 11:59 AM

Re: what is bind() method in javascript?
 
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

Nimmy Wilson 15th September 2015 03:48 AM

Re: what is bind() method in javascript?
 
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:


All times are GMT -6. The time now is 12:08 AM.