美文网首页
JS数组Array

JS数组Array

作者: desperadokk | 来源:发表于2019-01-05 20:08 被阅读0次

    Array不属于6种基本数据类型(null、number、undefined、boolean、string、symbol),所以Array属于复杂数据类型symbol。所以在创建变量时加不加new都是一样的




    var a = Array(3) //得到长度为3的数组,3个值都是undefined。
    var b = Array(3, 3) //得到长度为2的数组,2个值都是3。
    a.__proto__ === Array.prototype //true
    


    这就是数组的不一致性,只有一个参数时表示的是数组长度,大于一个参数时,表示的是数组的值。
    function也是对象,所以加不加new效果也是一样的

    var f = function(a, b) {
      return a + b
    }
    var f = new Function('a', 'b', 'return a + b')
    

    function关键字和Function()函数之间的区别

    function 关键字 if else var function
    Function 全局对象
    
    var 声明一个变量
    var a = 1
    function 声明一个函数
    function f(){}
    
    window.Object
    window.Function
    
    字符串String
    string
    var s = new String()
    
    声明函数的两种方法:
    
    具名函数
    function f(x, y){
          return undefined
    }
    等价于
    var f = function (){
          return undefined
    }
    等价于
    var f
    f = function (){
          return undefined
    }
    匿名函数
    function (){
          return undefined
    }
    Function声明函数
    new Function('x', 'y','x+y')
    
    

    数组和对象的区别


    本质区别在于对象的proto指向Object.prototype而数组的proto指向Array.prototype,原型链中没有Array.prototype的是伪数组。

    数组的forEach函数原理

    //forEach函数的源代码
    function forEach(array, x){
        for( let i = 0; i < array.length; i ++) {
            x(array[i], i)
        }
        // forEach没有return
    }
    
    forEach(['a', 'b', 'c'], function(value, key){
        console.log(value, key)
    })
    output:
    a 0
    b 1
    c 2
    
    var a = ['a', 'b', 'c']; 
    a.forEach(function(value, key){
        console.log(value, key)
    })
    output:
    a 0
    b 1
    c 2
    //所以
    a.forEach(function(value, key){
        console.log(value, key)
    })
    等价于
    forEach(a, function(value, key){
        console.log(value, key)
    })
    那a.forEach这个数组a是怎么传入forEach函数的呢?
    答案是通过this指针
    a.forEach = function(x) {
        for(let i = 0; i < this.length; i++) {
            x(this[i], i)
        }
    }
    a.forEach(function(value, key){console.log(value, key)})
    output:
    a 0
    b 1
    c 2
    

    a.forEach()需要传入一个函数作为参数,然后forEach函数再把传入的函数作为参数来运行forEach函数。

    数组的sort()函数

    a = [5, 6, 3, 4 1, 2]
    a.sort()
    // [1, 2, 3, 4, 5, 6]
    a
    // [1, 2, 3, 4, 5, 6]
    a.sort( function(x, y){ return x - y})
    // [1, 2, 3, 4, 5, 6]
    a.sort( function(x, y){ return y - x})
    // [6, 5, 4, 3, 2, 1]
    a = ['马云', '马化腾', '李彦宏']
    hash = {
        '马云': 167.92,
        '马化腾': 376,
        '李彦宏': 228.62
    }
    a.sort( function(x, y){ return hash[x] - hash[y]})
    //["马云", "李彦宏", "马化腾"]
    a.sort( function(x, y){ return hash[y] - hash[x]})
    //["马化腾", "李彦宏", "马云"]
    

    Array的其他API

    1. join
      join函数使数组变为字符串
    1. map
      map(function(){})和forEach(function(){})的函数唯一的区别就是forEach没有返回值,而map函数会返回参数函数function(){}对数组操作后的结果
    1. filter
      filter和map十分相似,返回参数函数为true的值


    2. reduce



    相关文章

      网友评论

          本文标题:JS数组Array

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