javascript - 使用未定义的变量/值

javascript里面一般不能使用未定义的值,但是下面几种情况除外:

 

1. 赋值语句中:

 

a=9;

 

alert(a)  //9

 

赋值语句中需要赋值的变量没定义会先定义,再赋值。另外从 a=b=c=8 不报错可以看出赋值语句是从右向左执行的。

 

2. for in语句中:

 

for(key in {name:'goofy'}){

alert(key)    //"name"

}

alert(key)    //"name"

 

 

for in语句中in左边的变量如果没定义也会先定义

 

3. typeof运算符后:

 

 

alert(typeof a) //'undefined'

 

alert(a)  //Uncaught ReferenceError: a is not defined

 

 

typeof 运算符可以跟未定义的值,但是不会去主动定义它

 

 

4. 对象属性:

 

 

var o={name:'goofy'}

 

alert(o.name)  // 'goofy'

 

o[age]=24; // Uncaught ReferenceError: age is not defined

 

alert(o.age)

 

 

在定义对象属性的时候如果是json直接量的形式可以使用没定义的值, 但是如果用下标的形式就会报错

 

5. function的参数:

 

function fn(a,b){

alert(a)  //4

alert(b)  //'undefined'

}

 

fn(4)

 

函数执行的时候会主动定义参数,所以在函数体内可以直接使用函数的参数,即时方法调用的时候这个参数并没有传递也不会报错

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Cannot destructure property 'Symbol(Symbol.iterator)' of 'item' as it is undefined. TypeError: Cannot destructure property 'Symbol(Symbol.iterator)' of 'item' as it is undefined. at eval (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./node_modules/@jiaminghi/data-view/lib/components/flylineChartEnhanced/src/main.vue?vue&type=script&lang=js&:337:23) at Array.map (<anonymous>) at VueComponent.calcflylinePoints (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./node_modules/@jiaminghi/data-view/lib/components/flylineChartEnhanced/src/main.vue?vue&type=script&lang=js&:335:35) at VueComponent.calcData (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./node_modules/@jiaminghi/data-view/lib/components/flylineChartEnhanced/src/main.vue?vue&type=script&lang=js&:293:7) at VueComponent.onResize (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./node_modules/@jiaminghi/data-view/lib/components/flylineChartEnhanced/src/main.vue?vue&type=script&lang=js&:284:7) at eval (webpack-internal:///./node_modules/@jiaminghi/data-view/lib/mixin/autoResize.js:44:57) at Array.eval (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:3008:12) at flushCallbacks (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2936:14)
最新发布
05-26
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值