Created
October 21, 2015 17:35
-
-
Save davydog187/dcafe2d9466313403cc8 to your computer and use it in GitHub Desktop.
explain how prototype works
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 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