0

currently I have to support the IE 9 so I cannot use classes and lambda expressions.

I have a function that represents my class and within this function I have another function for creating a new object.

For the scope I have to work with this but the problem is, there is no

  • new this.Obj()
  • this.Obj()
  • this new Obj()
  • new (this.Obj())

This is what I tried so far. I created a small example

function Store(originDogs) {

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

  this.originDogs = originDogs;

  this.dogs = this.originDogs.map(function(x) {
    return new this.Dog(x.name);
  });
}

new Store([{
  name: "dog1"
}, {
  name: "dog2"
}, {
  name: "dog3"
}]);

The error I get is

Uncaught TypeError: this.Dog is not a constructor

How do I have to Setup new this.Dog to make it work?

  • "I have to support the IE 9" — It has been over two & a half years since IE9 reached end of life and Microsoft stopped providing security fixes for it. It needs removing from whatever organisation you are supporting. – Quentin Aug 01 '18 at 09:59
  • @ChrisG — The question explicitly ruled arrow functions out due to lack of support in IE9. – Quentin Aug 01 '18 at 09:59
  • The function passed to map hijacks `this`; a common solution is using `var that = this;`, then using `that.Dog()` inside it. –  Aug 01 '18 at 10:01

0 Answers0