Uncaught TypeError: Cannot read property of undefined In JavaScript

 

当脚本遇到未初始化的变量或对象时,通常会抛出如上图所示的错误。

 

Decription

'Undefined'是全局对象的属性。如果没有为变量赋值,则为'undefined'类型。当求值变量没有任何赋值时,代码也会返回未定义的值。

 

Code structure

function test(t) {      //defining a function
  if (t === undefined) {       //if t=undefined, call tt
        console.log(t.tt)      //call tt member from t
  }
  return t;    
}
var a;    //a is a variable with undefined value
console.log(test(a)); //function call

 

Error

运行以上代码后,将会看到一个错误提示:

 

Debugging

需要在代码中判断是否变量是 undefined

if (typeof(jsvariable) == 'undefined') {
  ...
}

 

https://codeburst.io/uncaught-typeerror-cannot-read-property-of-undefined-in-javascript-c81e00f4a5e3

 

转载于:https://www.cnblogs.com/ryanzheng/p/10259719.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值