首页 > 解决方案 > 如何使用此构造函数中的另一个变量计算构造函数中的变量

问题描述

class CreateNewWord {
  constructor(id, title, hold, droppedHere, bg) {
    this.id = id;
    this.title = title; <=== I want to use this variable
    this.hold = hold;
    this.droppedHere = droppedHere;
    this.bg = bg;
    this.letters = getLetter(title); <=== I want to make something like this
  }

function getLetter(word) {
  let letters = [];
  for (let i = 0; i < word.length; i++) {
    letters.push({
      id: i + 1,
      title: word[i],
      hold: false,
      droppedHere: false,
      bg: "#a29bfe",
    });
  }
  return letters;
}

}

因此,当我想创建一个对象时,我不必传递字母,因为它是在类内部创建的

解决方案:

function  getLetter(word) {... 

getLetter(word) {... since I get and error if I use "function get..."

并写道:

this.letters = this.getLetter(title);

感谢“ponury-kostek”的帮助!

标签: javascriptoop

解决方案


推荐阅读