Skip to content

变量类型和计算 - 解答

仲灏约 1 分钟

变量类型和计算 - 解答

JS中使用typeof能得到的哪些类型

针对这个题目,可以通过以下程序进行验证

javascript
typeof undefined // 'undefined'
typeof 'abc' // 'string'
typeof 123 // 'number'
typeof true // 'boolean'
typeof Symbol('s') // 'symbol'
typeof {}  // 'object'
typeof [] // 'object'
typeof null // 'object'
typeof console.log // 'function'

何时使用=== 何时使用==

  • 所有的地方都用 ===
  • 除了判断是 null 或者 undefined 时用 if (obj.a == null) —— 这也是 jQuery 源码中的方式
js
const obj = { x: 100 }
if (obj.a == null) { }
// 相当于:
if (obj.a === null || obj.a === undefined) { }

值类型和引用类型的区别

js
// 值类型和引用类型的区别
const obj1 = { x: 100, y: 200 }
const obj2 = obj1
let x1 = obj1.x
obj2.x = 101
x1 = 102
console.log(obj1) // { x: 101, y: 200 }

手写深拷贝

不再重复写

上次更新:

讨论区

欢迎留下想法与补充