Below code demonstrates the usage of this and bind when dealing with objects:
// here this refers to the window object
function myFunc(){
console.log(this);
}
//myFunc();
var obj = {
objectFunction:myFunc
};
var Person = {
name: "Cassandra",
age: 27
}
obj.objectFunction.bind(Person)();
Refer:
Practice at:
http://jsbin.com
// here this refers to the window object
function myFunc(){
console.log(this);
}
//myFunc();
var obj = {
objectFunction:myFunc
};
var Person = {
name: "Cassandra",
age: 27
}
obj.objectFunction.bind(Person)();
Refer:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind
- https://stackoverflow.com/questions/2236747/use-of-the-javascript-bind-method
Practice at:
http://jsbin.com
No comments:
Post a Comment