bind() function javascript code example
Example 1: what is the use of bind method in javascript
"Variables has local and global scopes. Let's suppose that we have two variables
with the same name. One is globally defined and the other is defined inside a
function closure and we want to get the variable value which is inside the
function closure. In that case we use this bind() method.
code:
var x = 9;
var person = {
x: 81,
getX: function() {
return this.x;
}
};
var y = person.getX;
var myFunc = y.bind(person);
y();
myFunc();"
Example 2: bind (this)
this.getView().addEventDelegate({
onBeforeFirstShow: function() {
}.bind(this)
});