简体   繁体   English

为什么我越来越无法读取未定义错误的属性?

[英]why I am getting, cannot read property of undefined error?

This code 这段代码

 function Test(){ } new Test() ['width', 'height'].forEach(key => { console.log(key); }); 

gives me error 给我错误

在此处输入图片说明

but this code not 但是这段代码不是

 function Test(){ } new Test() var arr = ['width', 'height']; arr.forEach(key => { console.log(key); }); 

why? 为什么?

I am using chrome Version 53.0.2785.116 (64-bit) - Macbook air 2014 我正在使用Chrome版本53.0.2785.116(64位)-Macbook air 2014

As per snapshot, you are using chrome and Chrome supports .forEach from a long time. 根据快照,您使用的是chrome,Chrome很久以来就支持.forEach My guess is, issue is due to minification or missing semicolon( ; ) 我的猜测是,问题是由于缩小或缺少分号( ;

Sample 样品

 function test(){ this.name = "test"; } var a = new test()['width', 'height'].forEach(key => { console.log(key); }); 

You've omitted whatever is actually causing the problem from your code. 您已经从代码中省略了实际上导致问题的任何内容。

Immediately before the [ you must have something which refers to an object. [之前,您必须具有引用对象的内容。

This means that the [] syntax becomes the syntax to read a property value instead of the syntax to create an array. 这意味着[]语法成为读取属性值的语法,而不是创建数组的语法。

Then (because you are using a comma operator) 'width', 'height' resolves as 'height' so you are trying to read the height property. 然后(因为使用的是逗号运算符) 'width', 'height'解析为'height'因此您尝试读取height属性。

Since there is no height property on whatever the object is, you get undefined which doesn't have a forEach property. 由于无论对象是什么,都没有height属性,因此您将获得undefined属性,该属性没有forEach属性。

To fix this put a semicolon at the end of the previous statement . 要解决此问题,请在上一条语句的末尾加上分号

The second version of your code works because the var statement can't follow the syntax you have just before it, so it triggers automatic semicolon insertion . 代码的第二个版本有效,因为var语句无法遵循您之前的语法,因此会触发自动分号插入

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我在此 for 循环中无法读取未定义的属性“startsWith”? - Why am I getting Cannot read property 'startsWith' of undefined in this for loop? 为什么会出现“无法读取未定义的html属性”? - Why am I getting “Cannot read property of html undefined”? 为什么在此示例中出现“无法读取未定义的属性'文本'”? - Why am I getting “Cannot read property 'text' of undefined” in this example? 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 为什么我得到这个“无法读取未定义的属性”长度”? (JavaScript) - Why am I getting this 'cannot read property 'length' of undefined'? (JavaScript) 我收到一个错误:无法读取未定义的属性“推送” - I am getting an error: Cannot read property 'push' of undefined 我收到错误 Typerror 无法读取未定义的属性“执行” - I am getting an error Typerror Cannot read property 'execute' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM