lgy的博客


  • 首页

  • 标签

  • 分类

  • 归档

JS的引用类型

发表于 2018-11-08 | 分类于 JS

1.Object类型

创建Object实例的方式有两种。第一种是使用new操作符后跟Object构造函数,如下所示:

1
2
3
var person = new Object();
person.name = "Nicholas";
person.age = 29;

另一种是使用对象字面量表示法:

1
2
3
4
var person = {
name : "Nicholas",
age : 29
};

对象字面量语法要求的代码量少,而且整体性好,更受开发人员亲睐。

2.Array类型

创建数组的基本方式有两种,第一种是使用Array构造函数。

1
2
3
var colors1 = new Array();
var colors2 = new Array(20);
var colors3 = new Array("red","blue","green");

第二种是使用数组字面量表示法。

1
2
3
4
var colors = ["red","blue","green"];
alert(colors[0]); // 显示第一项
colors[2] = "black"; // 修改第三项
colors[3] = "brown"; // 新增第四项

数组的项数保存在其length属性中,而且这个属性可用来在数组末尾移除项和添加项。

1
2
3
4
var colors = ["red","blue","green"];
alert(colors.length); // 3
colors.length = 2; // 会移除最后一项
colors[colors.length] = "black"; // 末尾添加一项

数组的一些方法
1
2
3
4
5
6
7
8
9
10
11
12
var colors = ["red","blue","green"];
alert(colors.toString()); // red,blue,green
alert(colors.valueOf()); // red,blue,green
alert(colors.join('|')); // red|blue|green
//栈方法
alert(colors.push('yellow')); // 4,末端添加一项,返回长度
alert(colors.pop()); // "yellow",返回末端去掉的一项
alert(colors.length); // 3
//队列方法
alert(colors.unshift("yellow")); // 4,首端添加一项,返回长度
alert(colors.pop()); // "green",返回末端去掉的一项
alert(colors); // yellow,red,blue

重排序方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var values = [0,1,5,10,15];
values.reverse();
alert(values); // 15,10,5,1,0
values.sort();
alert(values); // 0,1,10,15,5
function compare(value1,value2){ // 升序比较函数
if (value1 < value2) {
return -1;
}else if (value1 > value2){
return 1;
}else {
return 0;
}
}
values.sort(compare);
alert(values); // 0,1,5,10,15

操作方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var colors = ["red","green","blue"];
var colors2 = colors.concat("yellow",["black","brown"]); // 将参数添加到副本末尾
alert(colors); //red,green,blue
alert(colors2); //red,green,blue,yellow,black,brown

var colors3 = colors2.slice(1,4); // 返回起始位置和结束位置(不包括)之间的项
alert(colors3); //green,blue,yellow

var removed = colors.splice(0,1); //删除0位置的1项
alert(colors); // green,blue
alert(removed); // red
var removed = colors.splice(1, 0, "yellow", "orange"); //位置1,插入2项
alert(colors); // green,yellow,orange,blue
alert(removed); // 空数组
var removed = colors.splice(1, 1, "red", "purple"); //位置1的1项改为2项
alert(colors); // green,red,purple,orange,blue
alert(removed); // yellow

迭代方法,包括every(),filter(),foreach(),map(),some()

1
2
3
4
5
var numbers = [1,2,3,4,5];
var result = numbers.forEach(function(item,index,array){
return (item+1);
});
alert(result); // 2,3,4,5,6

3.Function类型

每个函数都是Function类型的实例,而且与其他引用类型一样具有属性和方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
//函数声明
function sum1 (num1, num2) {
return num1 + num2;
}

//函数表达式
var sum2 = function(num1, num2){
return num1 + num2;
};

//函数名是一个指向函数对象的指针
var anotherSum = sum1;
alert(anotherSum(1,2)); // 3

解析器会率先读取函数声明,而函数表达式不会。

1
2
3
4
alert (sum(1,2));
var sum = function(num1, num2){
return num1 + num2;
}; //报错

函数的属性和方法

在函数内部有两个特殊的对象:arguments 和 this
arguments主要用于保存函数参数,该对象有一个callee的属性,指向拥有arguments对象的函数。

1
2
3
4
5
6
7
function factorial (num) {       //递归算法
if (num <=1) {
return 1;
}else {
return num * arguments.callee(num-1);
}
}

this引用是函数执行的环境对象,当在全局作用域中调用函数时,this对象引用的就是window。

1
2
3
4
5
window.color = "red";
function sayColor() {
alert (this.color);
}
sayColor(); //red

每个函数都包含两个属性: length 和 prototype ,length属性表示函数希望接收的参数的个数

1
2
3
4
function sum(num1, num2) {
return num1 + num2;
}
alert(sum.length); //2

每个函数都包含两个非继承而来的方法: apply() 和 call(),还有ES5定义的bind()方法

1
2
3
4
5
6
window.color = "red";
var o = { color: "blue" };
function sayColor() {
alert (this.color);
}
sayColor(o); //blue , 改变函数的作用域

4.基本包装类型

为方便操作基本类型值,ECMAScript 还提供了3个特殊的引用类型: Boolean , Number 和 String
实际上,每读取一个基本类型值的时候,后台就会自动创建一个对应的基本包装类型的对象,方便我们调用一些方法。这个自动创建的对象只存在于代码执行的瞬间,然后就会被销毁。

Boolean类型
1
2
3
4
5
6
7
8
9
//Boolean类型
var falseObject = new Boolean(false);
var result = falseObject && true;
alert(result); //true

//基本类型
var falseValue = false;
var result = falseValue && true;
alert(result); //false

由于Boolean对象是Boolean类型的实例,布尔表达式中对象会被转化为true;
Boolean对象和基本类型在使用typeOf 和 instanceof 返回值不同;
建议永远不要使用Boolean对象。

Number类型
1
2
3
4
5
6
var numberObject = new Number(10);
var numberValue = 10;
alert(typeOf numberObject); // "object"
alert(typeOf numberValue); // "number"
alert(numberObject instanceof Number); // true
alert(numberValue instanceof Number); //false

由于Number对象和基本类型数值在使用typeOf 和 instanceof 返回值不同,不建议实例化Number。

String类型

String类型是字符串的对象包装类型,可以使用String构造函数来创建

1
var stringObject = new String("Hello world");

String类型的每个实例都有一个length属性和许多方法

  1. 字符方法

    1
    2
    3
    var stringValue = "hello world";
    alert(stringValue.charAt(1)); // "e"
    alert(stringValue.charCodeAt(1)); // "101",对应字符的编码
  2. 字符串操作方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    var stringValue = "hello ";
    var result = stringValue.concat("world"); //拼接
    alert(result); // "hello world"
    alert(stringValue); // "hello "

    var stringValue = "hello world";
    alert(stringValue.slice(3,7)); // "lo w",7是到哪里之前停
    alert(stringValue.substring(3,7)); // "lo w",同上
    alert(stringValue.substr(3,7)); //"lo worl",7为个数
  3. 字符串位置方法

    1
    2
    3
    var stringValue = "hello world";
    alert(stringValue.indexOf("o",6)); // 7,正着从第6个开始数
    alert(stringValue.lastIndexOf("o",6)); // 4,倒着从第6个开始数
  4. trim() 方法

    1
    2
    3
    4
    var stringValue = "   hello world    ";
    var trimmedStringValue = stringValue.trim(); //删除前置及后缀空格
    alert(stringValue); // " hello world "
    alert(trimmedStringValue); // "hello world"

除此之外还有 字符串大小写转换方法,字符串模式匹配方法,localeCompare()方法等。

5.其他类型

  1. Date类型,提供日期和时间信息
  2. RegExp类型,是ES支持正则表达式的一个接口
  3. 作用域中的两个内置对象: Global 和 Math

参考书籍:JavaScript高级程序设计–【美】Nicholas C.Zakas

1…1516
ligaoyan

ligaoyan

16 日志
5 分类
1 标签
© 2019 ligaoyan
总访问量次 | 本站访客数人
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4