Instantly share code, notes, and snippets.

Embed
What would you like to do?
function Foo() {
if (!this.bar) {
this.bar = function () {
console.log('instance.bar');
};
}
}
// no bar method
Foo.prototype = {};
var foo1 = new Foo();
foo1.bar(); //instance.bar
// with bar method
Foo.prototype = {
bar: function () {
console.log('prototype.bar');
}
};
var foo2 = new Foo();
foo2.bar(); //prototype.bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment