Created
January 8, 2013 12:20
-
-
Save benqus/4483318 to your computer and use it in GitHub Desktop.
JavaScript inheritance behaviour testing
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
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