CodingTrain / CodingTrain/Wave-Function-Collapse
How to use constants as Object keys
- Ngôn ngữ chính
- JavaScript
- Star
- 218
- Fork
- 64
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
At [22:24](https://youtu.be/rI_y2GAlQFM?t=1365) 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.
```ts
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
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Hướng nghiên cứu
Xem lại phân đoạn video được tham chiếu, ví dụ p5.js được cung cấp và tài liệu tham khảo của MDN về computed property names. Cập nhật hướng dẫn liên quan của dự án để giải thích rằng dấu ngoặc vuông sử dụng các giá trị số của hằng số làm khóa đối tượng, sau đó xác minh rằng ví dụ đã sửa khớp với đầu ra được hiển thị.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript
- Lĩnh vực
- documentation
- Loại issue
- Tài liệu
- Độ khó
- 1/5
- Thời gian dự kiến
- Dưới một giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 42/100