0x08

Summer Training Day 9.

引用类型


引用类型


Object类型

创建方法:

1
2
3
//new操作符法.
var person = new Object(); <=> var person = {};
//Object()为构造函数.

1
2
3
4
5
6
7
8
9
10
11
12
13
//对象字面量法
var person = {
name : "John",
age : 30
};
//其中大括号表示对象字面量的开始,出现在表达式上下文中,
//表示一个表达式的开始.如果出现在语句上下文就表示语句的开始.
//其中属性名也可以用引号引起来,但是这样会导致其他数值也会自动转成字符串.
var person = {
"name" : "John",
"age" : 30
5 : true//这里的5会转换成字符串
};
1
2
3
4
5
6
7
8
9
10
//点表示法.(一般方法)
person.name
person.age
//方括号表示法.
alert (person["name"]);
//可以通过变量访问属性.
var propertyName = "name";//这里把属性赋给变量
alert (person[propertyName]);//John
//另外,在一些情况下不得不用方括号.
person["first name"] = "John"//这里有空格用点表示会有歧义.

Array类型

创建方法:

1
2
3
4
var colors = new Array(); <=> var colors = [];
var colors = new Array(20);//length = 20;
var colors = new Array("grey");//值为grey.
var colors = new Array("grey","red","blue");

数组的length属性不是只读的,可以移除或新增项.

1
2
3
4
5
6
7
var color = ["grey","red","blue"];
color.length = 2;//blue被删除.
color.length = 4;
alert(color[3]);//undefined.
//还可以实现类似push的功能.
var color = ["grey","red","blue"];
colors[colors.length] = "black";//末尾新增black.

p.s.数组最多可以包含4 294 967 295项.


检测数组:

1
2
3
4
5
6
7
var colors = [];
colors instanceOf Array;//True.
//但是instanceOf有缺陷,它假定只有一个全局执行环境.
//当网页有多个框架时就会有多个不同的全局执行环境.
//为了解决这个问题,ES5新增了Array.isArray(value)方法.
Array.isArray(colors);//True.
//此方法不管value在哪里创建的,就返回value是不是数组.


一些方法.

栈方法.LIFO后进先出

  • push()&pop();
    1
    2
    3
    4
    5
    6
    7
    8
    var colors = [];//创建数组
    var count = colors.push("red","white");//推入两项
    alert(count);//2
    var count = colors.push("black");//推入另一项
    var item = colors.pop();//弹出最后一项并取之.
    alert(item);//"black"
    alert(colors.length);//2

队列方法.FIFO先进先出

  • push()&shift()&unshift();
    1
    2
    3
    4
    5
    6
    7
    8
    var colors = [];//创建数组
    var count = colors.push("red","white");//推入两项
    alert(count);//2
    var count = colors.push("black");//推入另一项
    var item = colors.shift();//弹出第一项并取之.
    alert(item);//"red"
    alert(colors.length);//2
1
2
//与shift()相反的unshift()即是在第一项前面添加新项.
//unshift也与push对应.

操作方法.

  • concat()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    //concat.
    var colors = ["red","green","blue"];
    var colors2 = colors.concat("yellow",["black","brown"]);
    alert(color)//red,green,blue
    alert(color2)//red,green,blue,yellow,black,brown
    //若concat()无参数则只是复制当前数组并返回副本
    //若有一个或多个一维数组,则将它们每一项都添加进副本返回.
    //数组嵌套(n维数组)的情况,内层的作为一个n-1维数组返回.
    //若就一个数组则正常添加并返回.
  • slice()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var colors = ["red","green","blue","yellow","purple"];
    var colors2 = colors.slice(1);
    var colors3 = colors.slice(1,4);
    //若只有一个参数则返回从指定参数开始到数组末尾所有项.
    //若两个参数则返回起始位置到终止位置所有项但不包含终止位置项(左闭右开)"[ , )"
    alert(colors2);//green,blue,yellow,purple
    alert(colors3);//green,blue,yellow
    //当第二个参数为负数时,情况又有所不同.
    var colors4 = colors.slice(1,-1);
    //应该输出green,blue,yellow
    //负数是从右边数起的,正确理解应该是 -1 + 5 = 4
    // 负值 + arr.length
    //即colors.slice(1,-1); <=> colors.slice(1,4);

p.s.若起始位置大于终止位置(负值除外),则返回空数组。

  • spilce()
    据《JavaScript高级程序设计(第三版)》一书,splice()方法”算是最强大的数组方法”
    1
    2
    3
    4
    5
    Array.splice(position,deleteNumber,addThings);
    position//表示操作点位置.这个位置指向的是两个相邻项之间的空隙.
    deleteNumber//表示从操作点位置开始删除项的个数.
    addThings//表示从操作点开始插入的新项.
    该方法返回的是被删除的数组,若没有删除则返回空数组.

如:

1
2
3
4
5
6
7
8
9
10
11
12
var colors = ["red","green","blue"];
var removed = colors.splice(0,1);//从0位置开始删一项red
alert(colors);//green,blue
alert(removed);//red
var removed = colors.splice(1,0,"yellow","orange");//从1位置开始不删除并插入两新项
alert(colors);//green,yellow,orange,blue
alert(removed);//空
var removed = colors.splice(1,1,"red","purple");//从1位置开始删除yellow并插入两新项
alert(colors);//green,red,purple,orange,blue
alert(removed);//yellow

现在看来果不其然..

迭代方法.

以下方法均为对数组,每一项执行给定的函数.
且均不会改变原数组的值.

  • every(): 若每一项都返回True,则返回True.
  • some(): 任意一项返回True,则返回True.
  • filter(): 返回所有返回True的项构成的数组.
  • forEach(): 无返回值.
  • map(): 返回每次函数调用的结果构成的数组.

格式(以forEach为例):

1
2
3
num.forEach(function(item,index.array){
//操作.
});

归并方法.

  • reduce()&reduceRight()
    1
    2
    3
    4
    5
    6
    7
    8
    //reduce()从第一项开始逐项遍历到最后.
    //reduceRight()从最后一项开始遍历.
    //两者都是返回一个数值.
    格式:
    var values = [1,2,3,4,5];
    var sum = values.reduce(function(accumulate,current,index,array){
    return accumulate + current;
    });

Date类型

1
2
3
4
var now = new Date();
console.log(date);//当前时间;
getHours();//Minutes,Seconds,Month,FullYear
setHours();

以上.