Skip to content

Instantly share code, notes, and snippets.

@benqus
Created January 8, 2013 12:20
Show Gist options
  • Save benqus/4483318 to your computer and use it in GitHub Desktop.
Save benqus/4483318 to your computer and use it in GitHub Desktop.
JavaScript inheritance behaviour testing
var prototypeTest = (function () {
/**
* Instance class definition
* inherits from Base
*/
function Instance() {
//super class constructor
Base.apply(this, arguments);
}
/**
* Base class definition
*/
function Base(name) {
var privates = {
'name': name
};
this.getPrivate = function (name) {
return privates[name];
};
}
/**
* prorotype function
*/
Base.prototype.get = function () {
return this.getPrivate.apply(this, arguments);
};
/**
* resolving inheritance in the scope
*/
Instance.prototype = Base.prototype;
/**
* exposing 2 instances
*/
return {
i: new Instance('Geri'),
j: new Instance('Benő')
};
}());
/**
* short logging to check behaviour
*/
console.log(Object.getPrototypeOf(prototypeTest.i));
console.log(Object.getPrototypeOf(prototypeTest.i).set = function () {});
console.log(Object.getPrototypeOf(prototypeTest.i) === Object.getPrototypeOf(prototypeTest.j));
console.log(Object.getPrototypeOf(prototypeTest.j));
console.log(Object.getPrototypeOf(prototypeTest.i));
console.log(prototypeTest);
console.log(prototypeTest.i.get('name'));
console.log(prototypeTest.j.get('name'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment