Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

How to use constants as Object keys

未关闭
#35 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
1/5
预计耗时
1 小时以内
新手友好度
42/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
停滞
技术栈
javascript
领域
documentation

调研方向

查看所引用的视频片段、提供的 p5.js 示例以及 MDN 关于 computed property names 的参考资料。更新相关的项目指南,说明方括号使用常量的数值作为对象键,然后验证修正后的示例与所示输出一致。

由索引模型根据 Issue 内容生成。

描述

At 22:24 in the video Dan tries to use his constants as the keys in the object but it doesn't work for him. This is because he did it wrong.

when he did it you ended up with an object that had the strings "UP", "DOWN", etc as the keys but you wanted it to be the numbers that those constants represented. The way that you do that is to put the variables in square brackets. For example.

const UP = 0;
const DOWN = 1;
const LEFT = 2;
const RIGHT = 3;

const object1 = {
  UP: [],
  DOWN: [],
  LEFT: [],
  RIGHT: []
}
  
const object2 = {
  [UP]: [],
  [DOWN]: [],
  [LEFT]: [],
  [RIGHT]: []
}
  
  console.log(object1) => // {UP: Array(0), DOWN: Array(0), LEFT: Array(0), RIGHT: Array(0)}
  console.log(object2) => // {0: Array(0), 1: Array(0), 2: Array(0), 3: Array(0)}
}

and here's an interactive example https://editor.p5js.org/D_Snyder/sketches/r7rHl0QuA

The term for this is Computed Property Names and more details can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names

主要语言
JavaScript
星标
219
派生
64
PR 合并指标
30 天内没有已合并 PR

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

CodingTrain/Wave-Function-Collapse 的其他 Issue

查看 CodingTrain/Wave-Function-Collapse 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。