[英]How can I access properties that belong to the same object from inside the object in JavaScript?
Suppose that I have an object like this (it's a chart js setting).假设我有一个这样的对象(它是一个图表 js 设置)。
const radarChartData = {
labels: [3, 4, 6, 7, 8, 8],
data: [0, 0, 0, 0, 0, 0],
bgArr: this.labels.map((label, i) => label === 10 ? "red" : "blue"); // An error has occurred, because this code can't access its own labels in radarChartData.
datasets: [{.....}]
}
const radarChartConfig = {
type: ....,
}
const radarChart = new Chart(radarChartCtx, radarChartConfig)
I tried the following fix, but all failed...我尝试了以下修复,但都失败了......
bgArr: radarChartData.labels.map((label, i) => label === 10 ? "red" : "blue");
How to access its own property?如何访问自己的财产? In the first place, it's not a good option to access it and this way?
首先,以这种方式访问它不是一个好选择?
You can access the properties of an object in JavaScript in 3 ways: 1.Dot property accessor: object.在 JavaScript 中可以通过 3 种方式访问对象的属性: 1.Dot 属性访问器:对象。 property.
财产。 2.Square brackets property access: object['property'] 3.Object destructuring: const { property } = objec
2.方括号属性访问:object['property'] 3.Object解构:const { property } = objec
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.