zoukankan      html  css  js  c++  java
  • js-引用类型介绍

    object类型

    • 创建

        

    //使用构造函数
    var obj = new Object();
    person.name ='nick';
    person.son=21
    //对象字面量
    var obj ={
     name:'nick',
     age:21
    }
    1.    可以通过typeof来检测每个属性是否存在
    2. 访问方式:
      1. console.log(obj.name)
        console.log(obj['name'])

        如果属性名中包含导致语法错误的字符,或者属性名使用的是关键字或保留字,也可以使用方括号表示法

    Array类型

        js数组的大小是可以动态调整的,即可以随着数据的添加自动增长以容纳新的数据

    • 创建
      • //使用Array数组
        var color=new Array();
        //提前预知数组长度
        var color=new Array(20);
        //数组字面量
        var color = ['red','blue']
    • length属性

        它不是只读的

        

    var list = ['red','blue']
    console.log(list.length)//=>2
    list.length=3
    console.log(list[2])//=>undefined
    list.length=1;
    console.log(list[1])//=>undefined
    list[list.length]='blue'
    console.log(list)//=>list = ['red','blue']
    • 检测数组

         instanceof操作符

      

    console.log(list instanceof Array)//=>true

       ES5引入Array.isArray()方法

    console.log(Array.isArray[list])//=>true
    • 转换方法

        所有对象都具有toLocalString()、toString()、valueOf()方法

      

         let list = ['red','blue'];
            console.log(list.toString())
            console.log(list.valueOf());
            console.log(list.toLocaleString())

       valueOf()方法不同的是:将每一个值以字符串的形式表示,返回的是一个数组

      join()方法:可以使用不同的分隔符来构建这个字符串,如果不传入任何值,或传入undefined,则使用逗号作为分隔符

      

    console.log(list.join("/"))//=>red/blue
    console.log(list.join())//=>red,blue
    • 栈方法
    1.     push()方法:可以接收任意数量的参数,将它们逐个添加到数组的末尾,并返回修改后的数组长度
    2.               pop()方法:则从数组末尾移除最后一项,减少数组的个数,返回移除的值

      

    let list = ['red','blue'];
    console.log(list.push('green','black'))//=>4
    console.log(list)//=> ["red", "blue", "green", "black"]
    console.log(list.pop())//=>black
    console.log(list)//=>["red", "blue", "green"]
    • 队列方法
    1. shift()方法:移除数组中的第一个项并返回该项,同时将数组长度减1
    2. unshift()方法:能够在数组前端添加任意个数并返回新数组的长度
    let list = ['red','blue'];
    console.log(list.shift())//=>red
    console.log(list.unshift('red','purple'))//=>3
    console.log(list)//=>["red", "purple", "blue"]
    • 重排序算法
    1. reserve():反转数组项的顺序
    let list = ['red','blue'];
    console.log(list.reverse())//=>["blue", "red"]
    1. sort():按照升序排列数组项,该方法会调用每一个数组项的toString()方法,然后比较的得到的字符串,即使数组中的每一项都是数值,sort()方法比较的是字符串,这样会产生一个问题'10'会比‘5’小。

        

    function compare(value1,value2){
     if(value1<value2){
        return -1  //将-1与1调换排序就会不同
      }else if(value1>value2){
            return 1
        }else{
            return 0
        }
    }
    let arr = [1,5,0,10,15]
    value.sort(compare)
    console.log(arr)//=>[0,1,5,10,15]
    • 操作方法

        concat()方法:可以基于当前数组中的所有项创建一个新数组;这个方法先会创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组,在没有给concat()方法传递参数的情况下,它只是复制当前数组并返回副本。如果传递给concat()方法的是一个或多个数组,则该方法会将这些数组中的每一项都添加到结果数组中。如果传递的值不是数组,这些值就会被简单地添加到数组的末尾

    let list = ['red','blue'];
    let list1 = ['green'];
    console.log(list.concat(list1))//=>["red", "blue", "green"]

        slice()方法:可以基于当前数组中的一或多项创建一个新数组。slice()方法可以接受一或两个参数,即要返回项的起始和结束位置,不包括结束位置的项。在只有一个参数的情况下,返回参数指定位置开始到当前数组末尾的所有项。注意,该方法不影响原始数组

    let list = ['red','blue'];
    console.log(list.slice(1))//=>["blue"]

        splice()方法:主要用途是向数组的中部插入项

          删除:可以删除任意数量的项,只需指定2个参数:要删除的第一项的位置和要删除的项数

            

    let list = ['red','blue'];
    console.log(list.splice(1,1))//=>["blue"]
    console.log(list)//=>["red"]

          插入:可以向指定位置插入多个项,只需提供3个参数:起始位置、0(要删除的项数)、要插入的项

    let list = ['red','blue'];
    console.log(list.splice(1,0,'green','black'))//=>[]
    console.log(list)//=>["red", "green", "black", "blue"]

          替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定3个参数:起始位置、要删除的项数和要插入的任意数量的项

    let list = ['red','blue'];
    console.log(list.splice(1,1,'green','black'))//=>["blue"]
    console.log(list)//=>["red", "green", "black"]
    • 位置方法

        indexOf()方法:从数组的开头开始向后查找

        lastIndexOf()方法:从数组的末尾开始向后查找

    let list = ['red','blue','green','black'];
    console.log(list.indexOf('blue'))//=>1
    console.log(list.lastIndexOf('blue'))//=>1
     
    • 迭代方法

        every():对数组中的每一项都运行给定函数,如果该函数对每一项都返回true,则返回true

      

    let list = ['red','blue','green','black'];
    console.log(list.every((item,index,array)=>{
        return item.length>2
    }))//=>true
    console.log(list.every((item,index,array)=>{
        return item.length>3
    }))//=>false

        filter():对数组中的每一项都运行给定函数,返回该函数会返回true的项组成的数组

    let list = ['red','blue','green','black'];
    console.log(list.filter((item,index,array)=>{
        return item.length>3
    }))//=>["blue", "green", "black"]
    console.log(list.filter((item,index,array)=>{
        return item.length>2
    }))//=>["red", "blue", "green", "black"]

        forEach():对数组中的每一项都运行给定函数,该方法没有返回值

    let list = ['red','blue','green','black'];
    console.log(list.forEach((item,index,array)=>{
                //操作    
    }))

        map():对数组中的每一项都运行给定函数,返回每次函数调用的结果组成的数组

    console.log(list.map((item,index,array)=>{
        return item+'1'    
    }))//=>["red1", "blue1", "green1", "black1"]

        some():对数组中的每一项都运行给定函数,如果该函数对任一项返回true,则返回true

    console.log(list.some((item,index,array)=>{
        return item.length>2    
    }))//=> true
    • 归并方法

        reduce():从第一项开始,逐个遍历到最后

        reduceRight():从最后一项开始,向前遍历到第一项

        这两个方法都会迭代数组的所有项,然后构建一个最终返回的值。这两个方法都接受两个参数,一个在每一项调用的函数和(可选的)作为归并操作的初始值

       

  • 相关阅读:
    未能加载文件或程序集BUG系列
    寄语
    65. Valid Number
    56. Merge Intervals
    sublime text3 anaconda 插件报错
    42. Trapping Rain Water
    windows 下win+r无效
    93. Restore IP Addresses
    32. Longest Valid Parentheses
    48 Rotate Image
  • 原文地址:https://www.cnblogs.com/peilin-liang/p/12081558.html
Copyright © 2011-2022 走看看