patternjavascriptMinor
dnaof() - inheritance made easy, my own make inheritance tool
Viewed 0 times
madednaofmakeinheritanceeasyowntool
Problem
https://github.com/exebook/dnaof
I created this simple inheritance tool for JavaScript, could any one with deep knowledge in JavaScript prototyped inheritance review it?
Here is the library itself:
And here is the example usage to demonstrate what it can do:
I created this simple inheritance tool for JavaScript, could any one with deep knowledge in JavaScript prototyped inheritance review it?
Here is the library itself:
kindof = function(K) {
function X() {}
if (K != undefined) X.prototype = new K
X.can = X.prototype
return X
}
dnaof = function(x, f) {
var p = x.__proto__
x.__proto__ = p.__proto__
var r = p.__proto__[f].apply(x)
x.__proto__ = p
return r
}And here is the example usage to demonstrate what it can do:
require('./dnaof')
// create a kind of idiot without ancestor:
var idiot = kindof()
// tell what it can do:
idiot.can.say = function() { return this.name + ' can chat' }
idiot.can.rest = function() { console.log('bzzzz.z.z.z... (' + this.name + ')') }
// a new kind of smart inherits from a kind of idiot
var smart = kindof(idiot)
// he can also say something:
smart.can.say = function() {
// he can say something new, and he can say the same thing as an idiot can:
return dnaof(this, 'say') + ', ' + this.name + ' can talk'
}
// a kind of a genious can do the same things as an idiot and smart can, and even more:
var genious = kindof(smart)
genious.can.say = function() {
return dnaof(this, 'say') + ', ' + this.name + ' can discuss'
}
// instantiate three persons:
var bob = new idiot
var alice = new smart
var candy = new genious
// assign properties, because to make life simpler we do not initialize anything during creation:
bob.name = 'bob'
alice.name = 'alice'
candy.name = 'candy'
// let them talk:
console.log(bob.say())
console.log(alice.say())
console.log(candy.say())
// let them take some rest:
alice.rest(), bob.rest(), candy.rest()Solution
From a once over, and borrowing from the comments:
All in all, this is something I would not mind using, but that I would hate to have to maintain.
__proto__is bad news, MDN mentions this in a big red box of doom.
- There is no means to pass parameters to the constructor, that does not make it simple
X,x,k,randpare terrible variable names
dnaofis an unfortunate name
- lowerCamelCase is good for you,
kindof->kindOf,
- You can get the parent class thru
Object.getPrototypeOf(Object.getPrototypeOf(this))indnaof
- I have mixed feelings about the
cantrick. It is shorter than typingprototype, but it's also a candidate for nameclashes.
- You have no comments at all in your code, combined with 1 letter variables that makes for not good code
- If I were to build an OO library, I would also play with
constructorand add some support for private.
All in all, this is something I would not mind using, but that I would hate to have to maintain.
Context
StackExchange Code Review Q#43208, answer score: 2
Revisions (0)
No revisions yet.