Created
September 26, 2014 20:15
-
-
Save erikhazzard/0e36b48bbb5ac5e2d3ad to your computer and use it in GitHub Desktop.
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
function Chart(name){ | |
console.log('Yo new chart created'); | |
this.name = name; | |
this.nonPrototypeFunc = function(){ | |
console.log('what up chart : ' + this.name + ' || this: ', this); | |
}; | |
return this; | |
} | |
Chart.prototype.doStuff = function doStuff(){ | |
console.log('Doing stuff. Chart name: ' + this.name + ' || this context: ', this); | |
return this; | |
} | |
var chart1 = new Chart('A'); | |
var chart2 = new Chart('B'); | |
console.log('Prototype funcs are same reference to func in memory: Are they the same?', chart1.doStuff === chart2.doStuff); | |
console.log('Non Prototype funcs are individual function objects. Are they the same?: ', chart1.nonPrototypeFunc === chart2.nonPrototypeFunc); | |
chart1.doStuff(); // this context is chart 1 | |
chart2.doStuff.call(chart1); // this context is chart 1 - we're called doStuff, but passing in chart1 as the `this` context by using .call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment