How to dynamically create and pass an object

Viewed 113

I am trying to add and invoke function dynamically. For that, below is the code

filed = [];

function fileIt(thing) {
    // Dynamically call the file method of whatever
    thing.addIt();

    // Mark as filed
    filed.push(thing);
}

function repType1(name) {
    this.name = name;
}

repType1.prototype.addIt = function () {
    //Business Logic will go here
    console.log("Heloo From repType1");
}

function repType2(name) {
    this.name = name;
}

repType2.prototype.addIt = function () {
    //Business Logic will go here
    console.log("Heloo From repType2");
}

let rep1 = new repType1("repType1");
fileIt(rep1);

I am able to call function using let rep1 = new repType1("repType1"); for repType1but i need to do it dynamically so that same line of code can call my other function 'repType2' as well. I have the function parameter value as string for example: "repType1", "repType2", etc. How to do it?

2 Answers

Look like you are trying to mutate objects to perform something like the following If I understood you correctly :

const filed = [];

function fileIt(thing) {
  // Dynamically call the file method of whatever
  thing.addIt();

  // Mark as filed
  filed.push(thing);
}

class RepType {
   addItrepType1() {
     //Business Logic will go here
     console.log('Heloo From repType1');
   }
   
   addItrepType2() {
    //Business Logic will go here
    console.log('Heloo From repType2');
   }
   
   constructor(type) {
      // Set the function depending on the type
      this.addIt = this[`addIt${type}`];
      
      this.name = name;
   }
}

const rep1 = new RepType('repType1');
const rep2 = new RepType('repType2');

fileIt(rep1);
fileIt(rep2);


Though I would recommand you to use the inheritance system like :

const filed = [];

function fileIt(thing) {
  // Dynamically call the file method of whatever
  thing.addIt();

  // Mark as filed
  filed.push(thing);
}

// Abstract class
class ARepType {
  addIt() {
    throw new Error('Unimplemented abstract method');
  }

  constructor() {
    this.name = 'Abstract';
  }
}

class RepType1 extends ARepType {
  // override the function
  addIt() {
    //Business Logic will go here
    console.log('Heloo From repType1');
  }

  constructor() {
    super();

    this.name = 'repType1';
  }
}

class RepType2 extends ARepType {
  // override the function
  addIt() {
    //Business Logic will go here
    console.log('Heloo From repType2');
  }

  constructor() {
    super();

    this.name = 'repType2';
  }
}

const rep1 = new RepType1();
const rep2 = new RepType2();

fileIt(rep1);
fileIt(rep2);

Another possible way!

filed = [];

function fileIt(thing) {
    // Dynamically call the file method of whatever
    thing.addIt();

    // Mark as filed
    filed.push(thing);
}

function RepType() {
    RepType.types = {repType1, repType2};
    this.addIt = function () {
    };

    RepType.of = function (type) {
        return new (this.types[type])();
    };
}


function repType1(name) {
    this.name = name;
}

repType1.prototype = new RepType();
repType1.prototype.addIt = function () {
    //Business Logic will go here
    console.log("Heloo From repType1");
};

function repType2(name) {
    this.name = name;
}

repType2.prototype = new RepType();
repType2.prototype.addIt = function () {
    //Business Logic will go here
    console.log("Heloo From repType2");
};

let rep1 = RepType.of("repType1");

fileIt(rep1);

Related