最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to implement a named constructor in ES6 - Stack Overflow

programmeradmin0浏览0评论

I am trying to implement named constructor usage in ES6. The reason for this is that I thought it would be more pleasing to avoid the constructor invocation with new keyword and instead use a simple method of the class to make use of other methods. I thought of using a static function for this as a Proxy for the constructor.

I tried the following:

class Person {
  constructor(...props) {
    let {name, age} = props;
    this.name = name;
    this.age = age;
  }
  static create(...props) {
    return new Person(props);
  }
  
  display() {
    console.log(this)
  }
}

Person.create({name: 'John', age: 28}).display(); //Simple object input

I am trying to implement named constructor usage in ES6. The reason for this is that I thought it would be more pleasing to avoid the constructor invocation with new keyword and instead use a simple method of the class to make use of other methods. I thought of using a static function for this as a Proxy for the constructor.

I tried the following:

class Person {
  constructor(...props) {
    let {name, age} = props;
    this.name = name;
    this.age = age;
  }
  static create(...props) {
    return new Person(props);
  }
  
  display() {
    console.log(this)
  }
}

Person.create({name: 'John', age: 28}).display(); //Simple object input

But this won't work as simple object input gives:

Person {name: undefined, age: undefined}

Any help would be appreciated.

Update: Thanks, the answer by @appleapple helped a lot. I didn't notice the fact that I was passing a single argument. For those who are wondering how this could be done for n-Ary constructor method(of course using an object is neat, but still), here is an example:

class Person {
  constructor([name, age ]) {
    this.name = name;
    this.age = age;
  }
  static create(...props) {
    return new Person(props); //return new this(props); also works
  }
  
  display() {
    console.log(this)
  }
}

Person.create('John', 28).display();

Share Improve this question edited Apr 25, 2019 at 13:58 Patrick Roberts 52.1k10 gold badges117 silver badges163 bronze badges asked Apr 25, 2019 at 3:54 ubuntugodubuntugod 6521 gold badge9 silver badges16 bronze badges 1
  • constructor( [name, age] ) just provides people a way to create a person named '28' with age 'John'. most IDE's will provide information on parameters, if you give them sensible names, everyone's life will be easier for it. – increddibelly Commented Mar 26, 2021 at 14:02
Add a ment  | 

2 Answers 2

Reset to default 7

not that plex, object is a single argument, so just pass it around.

class Person {
  constructor(props) { // <-------
    let {name, age} = props;
    this.name = name;
    this.age = age;
  }
  static create(props) { // <-------
    return new Person(props); 
  }

  display() {
    console.log(this)
  }
}

Person.create({name: 'John', age: 28}).display();

reply to your update, actually you can forward the arguments (and I think the constructor looks better in this case)

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  static create(...props) {
    return new Person(...props); // <----------
  }
  
  display() {
    console.log(this)
  }
}

Person.create('John', 28).display();


or you can also take rest parameter in constructor (I don't like this, though)

class Person {
  constructor(...props) {  // <----------
    let [name, age]=props
    this.name = name;
    this.age = age;
  }
  static create(...props) {
    return new Person(...props); // <----------
  }
  
  display() {
    console.log(this)
  }
}

Person.create('John', 28).display();

发布评论

评论列表(0)

  1. 暂无评论