JS每周读书笔记[2]--Object & Array

来源:互联网 发布:2018年禁止网络直播 编辑:程序博客网 时间:2024/05/20 20:19

引用类型


  1. Object类型
  2. Array类型
  3. Date类型
  4. RegExp类型
  5. Function类型
  6. 基本包装类型
  7. 单体内置对象

1.Object

var person = {}person.name = "Nicholas";person.age = 29;alert(person["name"]); //"Nicholas"--优点是可以通过变量来访问属性alert(person.name); //"Nicholas"

2.Array

  • .length:
    数组的项数,length不是只读的,可以从数组的末尾移项或向数组中
    删除新项:

    var colors = ["red", "blue", "green"];colors.length = 2;alert(colors[2]);  //undefined "green"删除

    添加新项:

    var colors = ["red", "blue", "green"];colors[colors.length] = "black"; //在位置3增加blackcolors[colors.length] = "brown"; //在位置4增加brown
  • instanceof:
    确定某个对象是不是数组,
    用法:
    if (value instanceof Array){
    }
    但是如果网页有两个以上不同版本的Array不好区分。

  • .isArray()
    最终确定某个值是不是数组,
    用法:
    if (Array.isArray(value)){
    }

  • 数组转字符串.toString

    var colors = ["red", "blue", "green"];alert(colors.toString()); //red,blue,greenalert(colors.valueof()); //red,blue,greenalert(colors); //red,blue,green--alert()要接收字符串参数,会在后台调用toString()方法alert(colors.join(",")); //red,blue,greenalert(colors.join("||")); //red||blue||green
  • 栈方法 .push() .pop()
    LIFO:后进先出的数据结构
    push():添加到数组末尾,返回修改后的数组长度
    pop():从数组末尾移除一项,减少数组的length,返回移除的项

    var colors = new Array();var count = colors.push("red", "green");alert(count); //2 var item = colors.pop(); alert(item); //"green"
  • 队列 .push() .shift() .unshift()
    FIFO:先进先出的数据结构
    push():数组末端添加项
    shift():移除数组中第一项并返回该项
    unshift():在数组前端添加任意项并返回新数组的长度

    var colors = new Array();var count = colors.push("red", "green"); alert(count); //2 count = colors.push("black");alert(count);  //3var item = colors.shift(); //取得第一项alert(item); //"red"alert(colors.length); //2
  • 重排序方法 .sort() .reverse()
    reverse():反转数组的顺序
    sort():按从小到大排序,但是会受字符串比较影响

    function compare(value1, value2) {    if (value1 < value2) {        return -1;    } else if (value1 > value2) {        return 1;    } else {        return 0;    }}var values = [0, 1, 5, 10, 15];values.sort(compare);alert(values); //0,1,5,10,15
  • 操作方法 .concat() .slice() .splice()
    concat():从末尾拼接数组,返回新构建的数组
    slice():从指定位置到结束位置之间(不包括结束位置)的项,不影响原数组
    splice():可以删除、插入、替换数组

    var colors = ["red", "green"];var colors2 = colors.concat("yellow", "black");alert(colors2); // red, green, yellow, black ----------var colors = ["red", "green", "blue", "yellow", "purple"];var colors2 = colors.slice(1); //green, blue, yellow, purplevar colors3 = colors.slice(1,4); //green, blue, yellow----------var colors = ["red", "green", "blue"];var removed = colors.splice(0,1); //删除第一项alert(colors); //green, bluealert(removed); //redremoved = colors.splice(1, 0, "yellow", "orange"); //从位置1开始插入两项alert(colors); //green, yelloe, orange, bluealert(removed); //返回空数组
  • 位置方法 .indexOf() .lastIndexOf()
    indexOf(): 从数组开头向后查到项在数组中的位置,没有返回-1

  • 迭代方法 .every() .some() .filter() .forEach() .map()
    every(): 查数组中的每一项是否满足条件 true or false
    some(): 查数组中的是否有一项满足条件 true or false
    filter(): 筛选满足函数条件的数组 list
    map(): 数组中的每一项在对应的位置调函数 list
    forEach(): 数组中的每一项运行传入的函数 没有返回值 本质与for循环一样

  • 归并方法 .reduce() .reduceRight()
    reduce(): 从数组第一项开始,逐个遍历到最后
    reduceRight(): 从数组最后一项开始,向前遍历到第一项
    四个参数:前一个值,当前值,项的索引,数组对象。函数返回的值作为第一个参数传递给下一项。

    var values = [1,2,3,4,5]var sum = values.reduce(function(prev, cur, index, array){     return prev + cur;  });     alert(sum); //15
原创粉丝点击