javascript - 为什么 eval() 函数会产生未定义的属性?
问题描述
我正在尝试获取对象数组中的属性值,该数组是对象的属性-> this.form.data.output[0].output_total。数组名称、索引和属性名称作为参数传递并分配给变量。
我尝试过使用eval()
,并尝试登录到控制台,但结果是Cannot read property data of undefined
这是我调用该函数的方式:
(keyup)="form.onNumberChanges($event, 'output', 0, 'output_total')"
这是代码:
onNumberChanges: (event, params, index = null, properties = null) => {
if (index !== null) {
console.log(`this.form.data.${params}[${index}].${properties}`);
/* I am checking if the string isn't proper, but its result as same as I expected --> this.data.output[0].output_total */
console.log(eval(`this.form.data.${params}[${index}].${properties}`));
/* resulting cannot read property data of undefined */
}
我希望我能得到其中的值(0/2/3/或任何其他数字)。有什么线索吗?
解决方案
尝试将函数更改为下面,因为它看起来像与上下文(this)相关的问题,因为表单未定义
onNumberChanges(event, params, index = null, properties = null) {
并转换为
console.log(this.form.data[params][index][properties])
推荐阅读
- android - 一起改造请求@Part 和@FieldMap
- wordpress - Wordpress Themeforest (icloudconnected.com) 重定向?
- sql-server - 如何修改游标内的临时表?
- corda - 公证人将交易从验证更改为非验证公证人
- keras - 修改 resnet 模型中的图层
- sql - 在 MS Access 2016 中使用 JOIN 查询长文本格式的字段
- c - 仅颜色着色不同于文本+颜色着色
- angular - 许多简单 *ngIf 的单元测试
- delphi - TRichEdit 在更改自动换行时添加空白行(任何重新创建属性)
- java - kafka生产者API中的头信息