【前端面试指南】JS-1-类型

  1. 变量分为值类型和引用类型

  2. 值类型和引用类型的区别

    const obj1 = {х:100,у:200}
    const obj2 = obj1 
    let x1 = obj1.x
    obj2.x = 101
    x1 = 102
    console.log(obj1) // { x:101 }
    
  3. 对象、数组都是引用类型,null是特殊的引用类型,指针指向空地址

  4. 函数是一个特殊引用类型,并不用于存储数据,所以没有“拷贝、复制函数”的说法。

  5. typeof 运算符 :

    1. 识别所有的值类型
    2. 识别函数
    3. 识别是否是引用类型(不可再细分
    //判断所有值类型
    let a; typeof a // 'undefined'
    const str = 'abc' ; typeof str // 'string'
    const n = 100; typeof n // 'number'
    const b = true; typeof b // 'boolean'
    const S = Symbol('s'); typeof S // 'symbol' ES6
    
    //能判断函数
    typeof console.log // 'function'
    typeof function () {} // 'function'
    
    //能识别引用类型(不能再继续识别)
    typeof null // 'object '
    typeof ['a','b'] // 'object'
    typeof { x:100 } // 'object'
    
  6. 类型转换

    1. 字符串拼接

      const a = 100 + 10 // 110
      const b = 100 +'10' // ' 10010'
      const C = true + '10'// true10 !
      
  7. == 和 ===

    1. ==

    v2-56b0622eedf178434c31fafa34f7071b_r

    1. ===

    v2-a7d5eee8b2d41109a012647e8fec2d84_1440w

    1. 代码示例
    // == 运算符会做一些隐式的类型转换,尽量让两边相等
    100 == '100' // true
    0 == '' // true
    0 == false // true
    false == '' // true
    null == undefined // true
    
    // 三个等号会强制使两者一致,除了 == null 之外,其他都一律用===,例如:
    const obj = { x:100 }
    if (obj.a == null) { }
    // 相当于:
    // if (obj.a === null || obj.a === undefined) { }
    //除了 == null之外,其他都一律用 === , 例如:
    
    const obj = { x:100 }
    if (obj.a == null) { }
    // 相当于:
    // if (obj.a === null | | obj.a == undefined){}
    
  8. if语句和逻辑运算

    //以下是 falsely 变量。除此之外都是 truly 变量
    !!0 === false
    !!NaN ===false
    !!'' === false
    !!null === false 
    !!undefined === false 
    !!false === false
    
    console.log(10 && 0) // 0
    console.log( ''||'abc' ) // 'abc'
    console.log( !window. abc ) // true
    
  9. 小结

    1. 值类型 VS 引用类型、堆栈模型、深拷贝
    2. typeof 运算符
    3. 类型转换、truly 和 falsely 变量
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值