Skip to content

Instantly share code, notes, and snippets.

@davydog187
Created October 21, 2015 17:35
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 davydog187/dcafe2d9466313403cc8 to your computer and use it in GitHub Desktop.
Save davydog187/dcafe2d9466313403cc8 to your computer and use it in GitHub Desktop.
explain how prototype works
function test() {}
test.prototype.number = 5;
var a = new test
var b = new test
b.number // 5
b.hasOwnProperty("number") // false <- false because number is on the prototype
b.number = 10 // now were assigning to number, putting it on the instance
a.number // 5 <- hasn't changed, we haven't modified the prototype
b.hasOwnProperty("number") // true <- now this is true becasue its on the instance
delete b.number
b.hasOwnProperty("number") // false
b.number // 5 <- five again, because it finds "number" on the prototype
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment