0

I'm facing issues with configuring object while inheriting So I've

baseclass.prototype.activity =function(){ console.log("Im"+activity); }

and multiple other objects inheriting the base class(say A and B).

I want to configure run based on type of A and B So if it is A then calling run should make activity as Running and similarly with B it can be swimming I'm using following factory method for creating A and B depending on their type

activityFactoryObject.prototype.makeObj = Object.create(baseclass);
activityFactoryObject.prototype.createObject = function (config) {

    switch (config) {
    case "running":

        this.makeObj = A;
        break;
    case "swimming":

        this.makeObj = B;

        break;
    }
    this.makeObj.prototype = baseclass.prototype;  
    return new this.makeObj(config); 

};
Ecko123
  • 308
  • 1
  • 4
  • 16

2 Answers2

0

Im not sure exactly what your goal is but if they are both inheriting from baseClass and the only difference is what activity displays then something like this would be sufficient:

baseClass.protoype.activity = function ( activity ) { console.log( "I'm " + activity ); };

var a = new BaseClass(),
    b = new BaseClass();

a.activity( 'Running' ); // -> "I'm Running"
b.activity( 'Swimming' ); // -> "I'm Swimming"
kkemple
  • 982
  • 5
  • 9
  • Since it is a prototype , will both share a common instance ?? – Ecko123 Nov 27 '13 at 05:53
  • a common instance of the function, but it doesn't look like you are storing the activity they are doing in your code, just printing it. If you want to store it that would require more changes, im not sure you need child objects for this though, just instances and you could easily have a separate intstance of the activity and still share the same activitiy method, i would use a getter and setter. I will make a jsfiddel to explain better – kkemple Nov 27 '13 at 05:55
0

Not sure if this is what you're after:

var Activity = function(argsObj){
  this.activityType = argsObj.activityType || "Default value";
};
Activity.prototype.doIt = function(){
  console.log("I am " + this.activityType);
};

var Run = function(argsObj){
  //re use Activity initializer
  Activity.apply(this,arguments);
};
Run.prototype=Object.create(Activity.prototype);
Run.prototype.constructor = Run;

var Swim = function(argsObj){
  //re use Activity initializer
  Activity.apply(this,arguments);
};
Swim.prototype=Object.create(Activity.prototype);
Swim.prototype.constructor = Swim;

function activityFactory(argsObj){
  return new window[argsObj.type](argsObj);
}

var r = activityFactory({type:"Run",activityType:"running"});
var s = activityFactory({type:"Swim",activityType:"swimming"});
r.doIt();//I am running
s.doIt();//I am swimming
console.log(r instanceof Run);//true
console.log(s instanceof Swim);//true
console.log(s instanceof Activity);//true
var wrong = activityFactory({type:"Run"});
wrong.doIt();//I am Default value

More on constructor functions and prototype: https://stackoverflow.com/a/16063711/1641941

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160