Created
June 7, 2018 14:06
-
-
Save jmcmaster/dd98333759fa93f7b3eb17685bdbdcae to your computer and use it in GitHub Desktop.
this - Part 1 - Implicit Binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implicit Binding | |
// Left of the dot at call time | |
// Ask yourself... | |
// where is this function invoked? | |
// Example 1 | |
var me = { | |
name: 'Nate', | |
age: 25, | |
sayName: function() { | |
console.log(this.name); | |
} | |
}; | |
// me.sayName(); | |
// Example 2 | |
var person1 = { | |
name: 'Jeff', | |
age: 30 | |
}; | |
var person2 = { | |
name: 'Jason', | |
age: 32 | |
}; | |
var sayNameMixin = function(obj) { | |
obj.sayName = function() { | |
console.log(this.name); | |
}; | |
}; | |
sayNameMixin(person1); | |
sayNameMixin(person2); | |
person1.sayName(); | |
person2.sayName(); | |
// Example 3 | |
var Person = function(name, age) { | |
return { | |
name: name, | |
age: age, | |
sayName: function() { | |
console.log(this.name) | |
}, | |
mother: { | |
name: "Stacy", | |
sayName: function() { | |
console.log(this.name); | |
} | |
} | |
} | |
}; | |
var jim = Person('Jim', 36); | |
jim.sayName(); | |
jim.mother.sayName(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment