Skip to content

Instantly share code, notes, and snippets.

@erikhazzard
Created September 26, 2014 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikhazzard/0e36b48bbb5ac5e2d3ad to your computer and use it in GitHub Desktop.
Save erikhazzard/0e36b48bbb5ac5e2d3ad to your computer and use it in GitHub Desktop.
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