1
\$\begingroup\$

Depending on external OO libraries is really bad, using prototypes/new has some limitations, using only hashes has others. Dealing with classes was always a pain in JavaScript and it didn't help that every library has it's own way to deal with ti. But since some time I've finally "set" with this design:

function my_class(extend,init){
    var self = extend || {};

    var private = 5;
    self.public = 5;
    self.get_private = function(){
        return private;
    };

    for (var key in init) self[key] = init[key];
    return self;
};

Tests:

var instance = my_class();

// publics and privates
console.log(instance.public);
console.log(instance.get_private());

// methods never lose context
(function(method){
    console.log(method());
})(instance.get_private);

// multiple inheritance trivial
var random_obj = {a:1,b:2,c:3};
my_class(random_obj); // now random_obj inherits my_class

// serializable, store in a DB and recover it with:
var serialized = JSON.stringify(instance);
var instance = my_class({},JSON.parse(serialized));
console.log("Instance: ",instance);

I'm surprised how this simple design solves all the problems of the other approaches. Prototypes make multiple inheritance hard, hashes won't allow privates, there's no more the "this" binding issue, instances are JSON-serializable, all that without depending on external libraries. Is there any cons to this method, or something that is hard to do with it - or can I just go and start preaching about it?

\$\endgroup\$
7
  • \$\begingroup\$ What is the point of privates if anybody can retrieve them? Normally privates are only accessible to the actual methods, not to the public. \$\endgroup\$ – jfriend00 Apr 22 '13 at 20:10
  • \$\begingroup\$ What do you mean? It's just an example that privates are possible this way, it's not in some patterns. And privates have a point. \$\endgroup\$ – MaiaVictor Apr 22 '13 at 20:13
  • \$\begingroup\$ I'm asking what is the point of privates that are not private? You have an accessor for the privates that make it so anyone in the world can get access to them. They are not private. \$\endgroup\$ – jfriend00 Apr 22 '13 at 20:23
  • \$\begingroup\$ @jfriend00 What is not private? They're private, you can't access them without providing a public accessor. \$\endgroup\$ – MaiaVictor Apr 22 '13 at 20:25
  • \$\begingroup\$ @jfriend00 are you asking because I retrieved it's value on get_private()? In this case it could be useful to make a member-variable read-only, but it's just a demonstration. \$\endgroup\$ – MaiaVictor Apr 22 '13 at 20:28
1
\$\begingroup\$

What about regular inheritance? Is extend supposed to be the superclass?

foo = { foo: "bar" };

a = my_class(foo);
b = my_class(foo);

a.foo = "baz";
console.log(b.foo);

--> "baz";

I would expect a and b to have their own state, yet they share a single foo property.

\$\endgroup\$
6
  • \$\begingroup\$ No, maybe I expressed myself bad on that code. Extend is any object you want to transform into my_class. Init contains variables you want to overwrite over my_class's defaults. \$\endgroup\$ – MaiaVictor Apr 23 '13 at 0:36
  • \$\begingroup\$ @Dokkat - Okay, so how do you perform regular inheritance--extend the behavior of parent_class by adding/overriding those of my_class? This is the 99% of OOP that must exist before addressing the 1% of multiple-inheritance that can usually be implemented by other means (i.e. composition and interfaces). \$\endgroup\$ – David Harkness Apr 23 '13 at 3:35
  • \$\begingroup\$ Inheritance by overriding methods: sub_class = function(ext){ super_class(ext); ext.method = function(){}; return ext; }; now just call sub_class(obj) and it'll inherit super_class, except for that particular method, which will be of sub_class. \$\endgroup\$ – MaiaVictor Apr 23 '13 at 3:56
  • \$\begingroup\$ @David_Harkness and if you want to reuse the superclass's method, just replace the method definition I shown by something like var super_method = ext.method(); ext.method = function(){ super_method(); ... }; voila, you didn't overwrite method completely on inheritance, but combined both. No need for complications at all.... \$\endgroup\$ – MaiaVictor Apr 23 '13 at 3:58
  • \$\begingroup\$ @Dokkat - That looks rather complicated for handling the 99% case to make the 1% case a little easier. Perhaps a real-world example demonstrating inheritance, overriding, private/public properties and methods would help. \$\endgroup\$ – David Harkness Apr 24 '13 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.