0

I'm using typescript to design classes using the concept of Dependencies injection design pattern. Inside the code of Injector class there is line of code: car.apply(car, [new doors]); We suppose to execute the main class then we can use the (dependencies, methods) as we inject them as argument BUT the truth that the .apply does not execute the function!! So what is happening exactly ??

var car = function (){
    function car(doorsClass){
        this.doorsClass = doorsClass;
  this.color('red');
  this.doorsNum(4);
    }
    car.prototype.color = function(color){
     console.log('Car color is '+color);
    }
    car.prototype.doorsNum = function(doorsNum){
     console.log('Car has '+this.doorsClass.doors(doorsNum)+' doors');
    }
return car;
}

var doors = function (){
    function doors(){ }
    doors.prototype.doors = function(num){
     return num;
    }
return doors;
}

car.apply(car, [new doors]);
  • Your naming convention is terrible, making it hard to comment - but surely you need to execute the (outer) `m` function to get your inner `m` to `apply` – Jamiec Jun 14 '17 at 15:33
  • 1
    Sorry i have update the code to be more readable. Thanks for the note bro. – Ahmed B. Hameed Jun 14 '17 at 16:08

1 Answers1

0

There was a few things wrong with your code, mostly to do with never executing your modules, so your variable which should have pointed to the constructor actually pointed to the module.

The next big problem, was that you cannot call apply on the constructor to create a new instance. There are a number of workarounds, in this Q/A depending on your targeted Javascript version, but the below works (I think) how you intended.

var applyCtor = function(){
    var tempCtor = function() {};
    return function(ctor, args){
        tempCtor.prototype = ctor.prototype;
        var instance = new tempCtor();
        ctor.prototype.constructor.apply(instance,args);
        return instance;
    }
}();

var CarClass = (function (){
    function car(doorsClass){
        this.doorsClass = doorsClass;
    }
    car.prototype.color = function(color){
     console.log('Car color is '+color);
    }
    car.prototype.doorsNum = function(doorsNum){
     console.log('Car has '+this.doorsClass.doors(doorsNum)+' doors');
    }
    return car;
})(); //<-- execute your module 

var DoorsClass = (function (){
    function doors(){ }
    doors.prototype.doors = function(num){
     return num;
    }
    return doors;
})();//<-- execute your module 

// Apply the given ctor with arguments.
var carInstance = applyCtor(CarClass,[new DoorsClass()]);
carInstance.color('red');
carInstance.doorsNum(4);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Nice, work like charm. thank you Jamiec i really appreciate. – Ahmed B. Hameed Jun 14 '17 at 17:39
  • Hi Jamiec, Question please. After playing around trying to understand .apply method i was getting in to this car.prototype.constructor.apply(car.prototype, [new doors]); this code will give me the same result so why in your code create an empty function then attach all the prototypes into it then create instance of it ?? Is there a reason behind this ? Thanks – Ahmed B. Hameed Jun 15 '17 at 15:40
  • I didnt write it, I copied it from the linked answer. – Jamiec Jun 15 '17 at 15:51
  • Okay. Thanks a lot. – Ahmed B. Hameed Jun 15 '17 at 16:24