对象和数组

作者: sunny519111 | 来源:发表于2017-10-31 11:35 被阅读30次
    人生就像一列开往坟墓的列车,路途上会有很多站,很难有人至始至终陪你走完全程,当陪你的人要下车时,即便不舍,也要心存感激,然后挥手告别。---sunnyhuang

    对象和数组

    本文将从几个方面来探讨对象和数组的特点。对于方法的判定尽量不要用挂在全局window下面的方法。例如:isNaN(), parseInt()可以用Number.isNaN(), Number.parseInt()

    1. 判定类型
    2. 对象和数组的遍历
    3. 对象的一些常用方法
    4. 案例结合

    1.判定类型

    我们知道Javascript有6中基本类型(ES6的第七种symbol),简单的类型可以通过typeof运算符来判定基本类型,判定引用类型和null还是有一些小bug。

    1. typeof
    2. instanceof
    3. Object.prototype.toString.call()
    4. Number.isNaN()

    本文主要讲解判定数组和对象。

    1. 判定数字

    这是一个最简单的办法

    typeof 5  // "number"
    
    2. 判定数组
    function isArray(arr) {
      return  Array.isArray ? Array.isArray(arr) : Object.prototype.toString.call(arr) === '[object Array]'
    }
    
    3. 判定对象

    由于typeof null => object, 所有我们需要排除null

    function isObject(obj) {
      return typeof obj === 'object' || typeof obj === 'function' && !!obj
    }
    
    4. 黑科技区分数组和对象

    由于对象没有length,和数组却有length属性 ,利用这一点可以很方便的区分对象和数组。

    需求: 现在我们需要对于数组类型输出索引和值相加,对于对象需要输出key+value

    let arr = [1,2,3]  // sum(arr, console.log)  => 1 3 5
    let obj = {a: 'b', c: 'd'}  //sum(obj, console.log)  ab cd
    function sum(obj, callback, cxt) {
      let length = obj.length;
      if(length === +length) {
        arr.forEach((item,index) => {
          callback.call(cxt||this, item+index)
        })
      }else {
        Object.keys(obj).forEach(key => {
           callback.call(cxt||this, key+obj[key])
        })
      }
    }
    
    5. Object.prototype.toString.call() 一般用这个方法判定类型。

    对于面向对象编程,也有一切皆对象的说法,所有我们可以通过Object的原型链上面的toString()来判定类型。对于区分基本类型和引用类型很有效。

    function getTypeof(obj) {
      return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
    }
    // 基本类型
    getTypeof(1)  === 'number'  // true
    getTypeof(null)  === 'null'  // true
    getTypeof(undefined)  === 'undefined'  // true
    getTypeof(false)  === 'boolean'
    // 引用类型
    getTypeof([1])  === 'array'  // true
    getTypeof({})  === 'object'  // true
    getTypeof(function(){})  === 'function'  // true
    

    2.对象

    1. 对象定义属性Object.defineProperty()
    2. 获取对象的属性描述 Object.getOwnPropertyDescriptor(obj, props)
    3. 获取对象的所有属性(包括不可枚举的属性)Object.getOwnPropertyNames(obj)
    4. 判定属性是否是对象本身 obj.hasOwnProperty()
    对象遍历的三种方法对比
    1. for ... in... 缺点 : 会遍历原型链上面的属性,一般配合hasOwnProperty()
    2. Object.keys() 得到对象的keys数组,进行遍历。 缺点:获取不到不可枚举的属性
    3. Object.getOwnPropertyNames() 可以遍历对象的所有自身属性(包括不可枚举的)
    let arr = {a: 'b'}
    Object.prototype.test = 'test'
    Object.defineProperty(arr, 'c', {
      configurable: true,
      enumerable: fasle,  // 不可枚举
      writable: true,
      value: 'bb'
    })
    
    // for in
    for(let i in arr) {
      console.log(i)  // a test
    }
    
    // Object.keys()
    Object.keys(arr).forEach(key => {console.log(key)})) // a
    
    // Object.getOwnPropertyNames()
    Object.getOwnPropertyNames(arr).forEach(console.log)  // a c
    

    3.案例结合

    1. 判定有name和age的用户才是合法用户
    let condition = ['name', 'age']
    
    // obj = {name : 'cccc', age: 50}
    function valid(obj) {
      return condition.every(key => {
        return obj.hasOwnProperty(key)
      })
    }
    

    上面这样写,可以很好的分离出条件和方法,很方便的给我们添加额外的条件。

    相关文章

      网友评论

        本文标题:对象和数组

        本文链接:https://www.haomeiwen.com/subject/cixbpxtx.html