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 ()
//     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()

你可能感兴趣的:(JavaScript,javascript,前端)