0

I want a user to be able to create classes from a base class, and other user made classes, through one function given an object literal.

so there would be a list of classes:

var allclasses = {};

the parent class could look something like this, all classes have this ancestor: base = function() { this.name = 'base'; this.varname = 250; } base.prototype = { foo: function() { this.varname += 50; } } // added to allclasses allclasses['base'] = base;

and a method to create new classes:

function create(childliteral) { ???? }

an example of how this would work, ideally:

var myclass = create({
   parentname: 'base', // a required variable, create will return false if this isn't valid (not in allclasses)
   name: 'specialname', // a required variable, create will return false if this is already in allclasses
   foo: function() {
     this.varname += 100;
     // this is the part is what causes the most stress, I want it to call the parents function 'foo'
     this.parent.foo();
     return this.varname;
   }
});

and then to create an instance of the class:

var myobject = new myclass();

or..

var myobject2 = new allclasses['specialname'];

and finally, calling

myobject.foo();
console.log(myobject.varname) // prints out 400

myobject.parent.foo();
console.log(myobject.varname) // prints out 450
console.log(myobject.parent.varname) // also prints out 450
console.log(myobject.parent.name) // prints out 'base'
console.log(myobject.parentname) // prints out 'base'
console.log(myobject.name) // prints out 'specialname'

I have gotten extremely close to this, except I couldn't chain more than two objects.

dmills
  • 3
  • 4
  • Show us your current code, please! Notice that `this.parent.…` will loose the context of the current instance, so you might want to rethink that pattern. Also have a look at [`Class.js`](http://stackoverflow.com/a/15052240/1048572) to get inspired :-) – Bergi Feb 13 '14 at 16:02
  • Also, you might glance at T.J. Crowder's **[Lineage](https://code.google.com/p/lineagejs/)**. But be cautious about the use of the word "class" here; it often brings with it expectations from other languages not easily met by Javascript's prototype-based inheritance as well as restrictions from those languaged not present in JS. Besides, many JS people are prickly about it. :-) – Scott Sauyet Feb 13 '14 at 16:31
  • You need to better explain the options that you want to pass into the create method – ppoliani Feb 13 '14 at 16:32
  • @scottsauyet Thank you for that link! It's nice and lightweight. It's what I decided to go with. – dmills Feb 14 '14 at 14:44

1 Answers1

0

You need to somehow store a reference to the super class; for example

 var myclass = create({
    parentName: 'Base',
    name: 'specialname', 
    childCtor: function () { },
    childProto: {
      foo: function(){
         this.varname += 100;
         this._base_.foo(); // reference to the superclass
         return this.varname;
      }
    }
});

Look at the full example http://jsfiddle.net/zzu8J/7/

ppoliani
  • 4,792
  • 3
  • 34
  • 62