HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

dnaof() - inheritance made easy, my own make inheritance tool

Submitted by: @import:stackexchange-codereview··
0
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:

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:

  • __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, r and p are terrible variable names



  • dnaof is an unfortunate name



  • lowerCamelCase is good for you, kindof -> kindOf,



  • You can get the parent class thru Object.getPrototypeOf(Object.getPrototypeOf(this)) in dnaof



  • I have mixed feelings about the can trick. It is shorter than typing prototype, 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 constructor and 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.