Skip to main content
added 73 characters in body
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

I think your confusion is that you think that class constructors cannot be referenced by variables. They can, they're just functions. So:

class Foo {
    message() {
        console.log("I'm a Foo");
    }
}
class Bar {
    message() {
        console.log("I'm a Bar");
    }
}
function test(C) {
      let obj = new C();
      obj.message(); // "I'm a Foo" or "I'm a Bar", depending on
                     // whether Foo or Bar was passed in
}
test(Foo);
test(Bar);

Your pattern of calling var obj = Object.create(X.prototype) followed by X.apply(obj, /*...args here...*/) would work in ES5 and earlier, but ES2015's classes don't allow it. To construct instances from them, you have to use the new operator. The reason for that has to do with subclassing and setting new.target, so that if the instance has reason to create new instances (as Promise does), it can do that in an unambiguous way.

Which seems like it could be a step back, but if for some reason you have the constructor arguments as an array rather than discrete items, spread notation lets you use new anyway:

let obj = new C(...args);

So if you need a generic function that accepts a class constructor and an array of arguments and needs to return an instance of that class using those arguments, it would look like this:

function createInstanceOf(C, args) {
    return new C(...args);
}

I think your confusion is that you think that class constructors cannot be referenced by variables. They can, they're just functions. So:

class Foo {
    message() {
        console.log("I'm a Foo");
    }
}
class Bar {
    message() {
        console.log("I'm a Bar");
    }
}
function test(C) {
      let obj = new C();
      obj.message(); // "I'm a Foo" or "I'm a Bar", depending on
                     // whether Foo or Bar was passed in
}
test(Foo);
test(Bar);

Your pattern of calling var obj = Object.create(X.prototype) followed by X.apply(obj, /*...args here...*/) would work in ES5 and earlier, but ES2015's classes don't allow it. To construct instances from them, you have to use the new operator.

Which seems like it could be a step back, but if for some reason you have the constructor arguments as an array rather than discrete items, spread notation lets you use new anyway:

let obj = new C(...args);

So if you need a generic function that accepts a class constructor and an array of arguments and needs to return an instance of that class using those arguments, it would look like this:

function createInstanceOf(C, args) {
    return new C(...args);
}

I think your confusion is that you think that class constructors cannot be referenced by variables. They can, they're just functions. So:

class Foo {
    message() {
        console.log("I'm a Foo");
    }
}
class Bar {
    message() {
        console.log("I'm a Bar");
    }
}
function test(C) {
      let obj = new C();
      obj.message(); // "I'm a Foo" or "I'm a Bar", depending on
                     // whether Foo or Bar was passed in
}
test(Foo);
test(Bar);

Your pattern of calling var obj = Object.create(X.prototype) followed by X.apply(obj, /*...args here...*/) would work in ES5 and earlier, but ES2015's classes don't allow it. To construct instances from them, you have to use the new operator. The reason for that has to do with subclassing and setting new.target, so that if the instance has reason to create new instances (as Promise does), it can do that in an unambiguous way.

Which seems like it could be a step back, but if for some reason you have the constructor arguments as an array rather than discrete items, spread notation lets you use new anyway:

let obj = new C(...args);

So if you need a generic function that accepts a class constructor and an array of arguments and needs to return an instance of that class using those arguments, it would look like this:

function createInstanceOf(C, args) {
    return new C(...args);
}
added 237 characters in body
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

I think your confusion is that you think that class constructors cannot be referenced by variables. They can, they're just functions. So:

class Foo {
    message() {
        console.log("I'm a Foo");
    }
}
class Bar {
    message() {
        console.log("I'm a Bar");
    }
}
function test(C) {
      let obj = new C();
      obj.message(); // "I'm a Foo" or "I'm a Bar", depending on
                     // whether Foo or Bar was passed in
}
test(Foo);
test(Bar);

Your pattern of calling var obj = Object.create(X.prototype) followed by X.apply(obj, /*...args here...*/) would work in ES5 and earlier, but ES2015's classes don't allow it. To construct instances from them, you have to use the new operator.

Which seems like it could be a step back, but if for some reason you have the constructor arguments as an array rather than discrete items, spread notation lets you use new anyway:

let obj = new C(...args);

So if you need a generic function that accepts a class constructor and an array of arguments and needs to return an instance of that class using those arguments, it would look like this:

function createInstanceOf(C, args) {
    return new C(...args);
}

I think your confusion is that you think that class constructors cannot be referenced by variables. They can, they're just functions. So:

class Foo {
    message() {
        console.log("I'm a Foo");
    }
}
class Bar {
    message() {
        console.log("I'm a Bar");
    }
}
function test(C) {
      let obj = new C();
      obj.message(); // "I'm a Foo" or "I'm a Bar", depending on
                     // whether Foo or Bar was passed in
}
test(Foo);
test(Bar);

I think your confusion is that you think that class constructors cannot be referenced by variables. They can, they're just functions. So:

class Foo {
    message() {
        console.log("I'm a Foo");
    }
}
class Bar {
    message() {
        console.log("I'm a Bar");
    }
}
function test(C) {
      let obj = new C();
      obj.message(); // "I'm a Foo" or "I'm a Bar", depending on
                     // whether Foo or Bar was passed in
}
test(Foo);
test(Bar);

Your pattern of calling var obj = Object.create(X.prototype) followed by X.apply(obj, /*...args here...*/) would work in ES5 and earlier, but ES2015's classes don't allow it. To construct instances from them, you have to use the new operator.

Which seems like it could be a step back, but if for some reason you have the constructor arguments as an array rather than discrete items, spread notation lets you use new anyway:

let obj = new C(...args);

So if you need a generic function that accepts a class constructor and an array of arguments and needs to return an instance of that class using those arguments, it would look like this:

function createInstanceOf(C, args) {
    return new C(...args);
}
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

I think your confusion is that you think that class constructors cannot be referenced by variables. They can, they're just functions. So:

class Foo {
    message() {
        console.log("I'm a Foo");
    }
}
class Bar {
    message() {
        console.log("I'm a Bar");
    }
}
function test(C) {
      let obj = new C();
      obj.message(); // "I'm a Foo" or "I'm a Bar", depending on
                     // whether Foo or Bar was passed in
}
test(Foo);
test(Bar);