Skip to content

Instantly share code, notes, and snippets.

@fortunee
Created June 9, 2019 18:44
Show Gist options
  • Save fortunee/3a21b226f244731473dd45576919d3cc to your computer and use it in GitHub Desktop.
Save fortunee/3a21b226f244731473dd45576919d3cc to your computer and use it in GitHub Desktop.
The fifth rule of javascript this keyword
// Rule #5 example
function RuleFive() {
this.value = 'Some value for rule #5';
this.printValue = function() {
console.log('Rule five value >>>>', this.value);
}
}
const ruleFiveInstance = new RuleFive();
/**
* Rule #2 call method takes precedence here
* even though dot notation of rule #3 applies
*/
ruleFiveInstance.printValue.call({ value: 'Another bounded val'});
// Rule #1 new keyword takes precedence even tho rule #3 applies
new ruleFiveInstance.printValue();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment