JavaScript中你不知道的undefined

// 'use strict';
// undefined 既是一个原始数据类型,也是一个原始值数据
// undefined 全局对象上的一个属性  window.undefined
console.log('%c Line:5 🍋 window.undefined', 'color:#ffdd4d', window.undefined) // undefined

// 不可写   writable: false
window.undefined = 1
console.log('%c Line:10 🍞 window.undefined', 'color:#6ec1c2', window.undefined) // undefined

// // 不可配置 configurable: false
delete window.undefined
console.log('%c Line:15 🍫 window.undefined', 'color:#ffdd4d', window.undefined) // undefined

// 不可枚举   enumerable: false
for (var k in window) {
  if (k === undefined) {
    console.log('%c Line:18 🥤 k', 'color:#2eafb0', k)
  }
}

// 不可重新定义
// Object.defineProperty(window, 'undefined', {
//   enumerable: true,
//   writable: true,
//   configurable: true,
// })
// index.js:23 Uncaught TypeError: Cannot redefine property: undefined
//     at Function.defineProperty (<anonymous>)
//     at index.js:23:8

var a

// 系统会给一个未赋值的变量自动赋值为undefined,类型也是undefined
console.log('%c Line:35 🍊 a', 'color:#2eafb0', a) // undefined
console.log('%c Line:36 🌶 typeof a', 'color:#42b983', typeof a) // undefined

function test(a) {
  console.log(typeof a) // undefined

  return a // undefined
}

console.log('%c Line:44 🥐 test()', 'color:#33a5ff', test()) // undefined

function test() {
  console.log(123)
  // 函数内部没有显示返回一个值的时候,系统默认给函数返回undefined
}

console.log('%c Line:52 🥐 test()', 'color:#33a5ff', test()) // undefined

var undefined = 1
console.log('%c Line:54 🍑 window.undefined', 'color:#f5ce50', window.undefined) //undefined

function test() {
  // 'use strict';
  // undefined不是JS的保留字和关键字
  var undefined = 1
  console.log('%c Line:60 🌶 undefined', 'color:#f5ce50', undefined) // 1
}
test()

var a = null

// a === undefined false
if (a === undefined) {
  console.log(true)
} else {
  console.log(false)
}

var a
console.log('%c Line:76 🍪 typeof a', 'color:#3f7cff', typeof a) // object
if (typeof a === 'undefined') {
  console.log(true)
} else {
  console.log(false)
}

console.log(typeof b) // undefined

var a

if ('a' in window) {
  // true
  console.log(true)
} else {
  console.log(false)
}

// void(0) ->对0进行求值 返回undefined

var a, b, c

a = void ((b = 1), (c = 2))

console.log('%c Line:98 🍒 a, b, c', 'color:#e41a6a', a, b, c) // undefined 1 2
console.log(
  '%c Line:100 🍉 void(0) === window.undefined',
  'color:#ea7e5c',
  void 0 === window.undefined
) // true

function test() {
  var undefined = 1
  console.log(undefined) // 1
  console.log(window.undefined === void 100) // true
  console.log(undefined === void 0) // false
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值