Last active
July 25, 2020 02:00
-
-
Save a-laughlin/b015f869cafad718f0540873438d3713 to your computer and use it in GitHub Desktop.
prototype chain
This file contains 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
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(); | |
(function(){ | |
'use strict'; | |
// calling stored method defined with function syntax prototype inheritance in strict mode: error | |
// calling stored method defined with class syntax prototype inheritance (always strict mode): error | |
// calling stored method defined with function syntax prototype inheritance in non-strict mode: this===window | |
// ^ try commenting out 'use strict' above, then running it | |
function FMammal (name){ | |
this.name=name; | |
} | |
FMammal.prototype.getProtoString = function(logLabel){ return protoString(this) } | |
function FPet (name){ | |
FMammal.call(this,name); | |
}; | |
FPet.prototype=Object.create(FMammal.prototype); | |
FPet.prototype.constructor = FPet; | |
const fpet = new FPet('Clyo'); | |
console.log('fpet.getProtoString()',fpet.getProtoString()); | |
const fgetProtoString = fpet.getProtoString; | |
const bar={name:'bar',baz:function(){try{console.log('baz',fgetProtoString())}catch(e){console.log(`baz fgetProtoString() error ${e.message}`)}}}; | |
bar.baz(); | |
// ^ retains the global execution context it's assigned to even when called from within another method. | |
// execution context and "this" binding are unrelated | |
// calling context object and "this" binding are related | |
try{console.log('fgetProtoString()',fgetProtoString())}catch(e){console.log(`fgetProtoString() error ${e.message}`);} | |
class CMammal { | |
constructor(name){this.name=name;} | |
getProtoString(){ return protoString(this) } | |
} | |
class CPet extends CMammal { | |
constructor(name){super(name);} | |
} | |
const cpet = new CPet('Clyo'); | |
console.log('cpet.getProtoString()',cpet.getProtoString()); | |
const cgetProtoString = cpet.getProtoString; | |
try{console.log('cgetProtoString()',cgetProtoString())}catch(e){console.log(`cgetProtoString() error ${e.message}`);} | |
function* yieldProto (o){while(o=o.__proto__) yield o.constructor.name;yield 'null';} | |
function protoString(o){return [...yieldProto(o)].reverse().join(" < ")}; | |
}()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment