Skip to content

Instantly share code, notes, and snippets.

@tomachalek
Created July 30, 2014 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomachalek/187fd3e18260ef04ad8a to your computer and use it in GitHub Desktop.
Save tomachalek/187fd3e18260ef04ad8a to your computer and use it in GitHub Desktop.
JavaScript prototype inheritance quiz
(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