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.