简体   繁体   English

如何解构对象并将结果作为参数传递给类构造函数?

[英]how to destructure an object and pass the result as argument in a class constructor?

class Cat {
  constructor(words, feet) {
    //this.meow(words);
    this.words = words;
    this.feet = feet;
  }
  meow() {
    console.log(`meow${this.words}${this.feet}`);
  }
}

class Tiger {
  constructor(words) {
    this.words = words;
    //this.rowr(words);
  }
  rowr() {
    console.log(`rowr${this.words}`);
  }
}
const param = {
  words: 'abc',
  feet: 2,
}
const cat = new Cat(...param);
console.log(cat.meow())

I got meowundefinedundefined 我得到了meowundefinedundefined

Why is that? 这是为什么? Note that I have es6 babel enabled. 请注意,我已启用es6 babel。

You are trying to spread an object into the params. 您正在尝试将对象传播到参数中。 You can only spread an object into another object. 您只能将一个对象散布到另一个对象中。 To destructure it use curly braces in the constructor definition. 要对其进行分解,请在构造函数定义中使用花括号。

Note: object rest/spread is still a stage 3 proposal , and requires a babel transform to work. 注意:对象休息/传播仍然是第3阶段的提议 ,并且需要babel转换才能起作用。

 class Cat { constructor({ words, feet }) { // destructure the object //this.meow(words); this.words = words; this.feet = feet; } meow() { console.log(`meow${this.words}${this.feet}`); } } class Tiger { constructor(words) { this.words = words; //this.rowr(words); } rowr() { console.log(`rowr${this.words}`); } } const param = { words: 'abc', feet: 2, } const cat = new Cat(param); cat.meow() // meow doesn't return anything, so console.log it will give you undefined 

Another option is to destructure it outside, and then assign it to the constructor: 另一种选择是在外部对其进行解构,然后将其分配给构造函数:

 class Cat { constructor(words, feet) { //this.meow(words); this.words = words; this.feet = feet; } meow() { console.log(`meow${this.words}${this.feet}`); } } class Tiger { constructor(words) { this.words = words; //this.rowr(words); } rowr() { console.log(`rowr${this.words}`); } } const { words, feet } = { words: 'abc', feet: 2, } const cat = new Cat(words, feet); cat.meow() // meow doesn't return anything, so console.log it will give you undefined 

You can't destructure an object like that to fill params for a method. 您不能通过分解对象来填充方法的参数。 you should rather try destructuring an array to pass to the constructor or change the constructor to receive an object instead. 您应该尝试解构数组以传递给构造函数,或更改构造函数以接收对象。 words and feet are undefined because they never get passed to the constructure as you would expect. 单词和脚没有定义,因为它们从未像您期望的那样传递到结构中。

 class Cat { constructor(words, feet) { //this.meow(words); this.words = words; this.feet = feet; } meow() { console.log(`meow${this.words}${this.feet}`); } } class Tiger { constructor(words) { this.words = words; //this.rowr(words); } rowr() { console.log(`rowr${this.words}`); } } const p = ['abc', 2]; const icat = new Cat(...p); //right icat.meow(); // meowabc2 

Checkout this codepen . 签出此codepen

Try spreading an array items. 尝试散布数组项。 Eg: 例如:

const words = 'abc';
const feet = 2;
const param = [
    words,
    feet
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM