Skip to content

Instantly share code, notes, and snippets.

@3emad
Last active August 29, 2015 14:13
Show Gist options
  • Save 3emad/0563a03464ca72c5defc to your computer and use it in GitHub Desktop.
Save 3emad/0563a03464ca72c5defc to your computer and use it in GitHub Desktop.
A small experiment of prototypes
var a = function a(){
this.x = 1;
return this;
}
var l = {
x:1
};
a.prototype.inc = function(){ return this.x+1; }
l.inc = function(){ return this.x+1; }
var b = new a();
var c = a();
console.log('b =',typeof b, b);
console.log('c =',typeof c, c);
console.log('l =',typeof l, l);
console.log('b.x =', b.x);
console.log('b.inc() = ',b.inc());
console.log('l.x =', l.x);
console.log('l.inc() = ',l.inc());
console.log('c.x = ', c.x);
console.log('c.inc() = ',c.inc());
/*
b = object a {x: 1, inc: function}
c = object Window {...}
l = object Object {x: 1, inc: function}
b.x = 1
b.inc() = 2
l.x = 1
l.inc() = 2
c.x = 1
Uncaught TypeError: undefined is not a function
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment