Created
July 30, 2014 07:11
-
-
Save tomachalek/187fd3e18260ef04ad8a to your computer and use it in GitHub Desktop.
JavaScript prototype inheritance quiz
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 () { | |
'use strict'; | |
var foo, bar1, bar2; | |
function Foo () { | |
this.counter = { i : 0}; | |
this.counter2 = 0; | |
} | |
function Bar() { | |
} | |
foo = new Foo(); | |
Bar.prototype = foo; | |
Bar.prototype.sayHello = function () { | |
console.log('sayHello(): counter1 =', | |
this.counter.i, | |
', counter2 =', | |
this.counter2, | |
'\n'); | |
this.counter.i += 1; | |
this.counter2 += 1; | |
}; | |
bar1 = new Bar(); | |
bar2 = new Bar(); | |
foo.sayHello(); | |
bar1.sayHello(); | |
bar2.sayHello(); | |
foo.sayHello(); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment