javascript - 如何使用此构造函数中的另一个变量计算构造函数中的变量
问题描述
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”的帮助!
解决方案
推荐阅读
- registry - Edge-Chromium GP - 如何将 google 设置为默认值、绕过首次运行对话框以及没有新闻的标签页?
- android - Flutter with Firebase Google 登录:断言失败 network_image_io.dart
- pandas - Pandas:如何使用单独的 y 轴进行绘图,但仍控制顺序?
- pandas - 名称 _stringify_path 未定义
- amazon-web-services - SQS 策略不授予访问权限
- python - 运算符作为 IF 的参数返回 SyntaxError: invalid syntax
- python - Python中的地理插值
- excel - VBA 选择表中符合条件的数据
- android - 服务内的 startActivity 不适用于所有 android 设备
- firebase - Go 是否支持实时 Firebase 事件?