Skip to content

Instantly share code, notes, and snippets.

@a-laughlin
Created July 25, 2020 01:57
Show Gist options
  • Save a-laughlin/06ef449848077d6c5cb30452016187fd to your computer and use it in GitHub Desktop.
Save a-laughlin/06ef449848077d6c5cb30452016187fd to your computer and use it in GitHub Desktop.
prototype chain
var mufasa = {
name: "Mufasa",
sing() {
var inner = function() {
console.log("inner:this", this);
};
var innerStrict = function() {
'use strict';
console.log("innerStrict:this", this);
};
var innerArrow = ()=>console.log("innerArrow:this", this)
var innerArrowStrict = ()=>{
'use strict'; // doesn't matter in an arrow function
console.log("innerArrowStrict:this", this);
}
console.log("sing", this);
inner();
innerStrict();
innerArrow();
innerArrowStrict();
}
};
var simba = {name:'Simba'};
console.log('\nmufasa.sing(); ... calling sing in the context of mufasa')
mufasa.sing();
console.log('\nmufasa.sing.call(simba); ... calling sing in the context of simba')
mufasa.sing.call(simba);
console.log('\nsimba.sing=mufasa.sing;simba.sing(); ... assigning sing to a different context')
simba.sing=mufasa.sing;simba.sing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment