【JavaScript高级程序设计】3、引用类型
---恢复内容开始---
1、Object类型
对象类型的初始化方式
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<script type="text/javascript">
var person = new Object();
person.name = "cutter_point";
person.age = 22;
alert(person.name + "==" + person.age);
var person2 =
{
name : "cutter_point2",
age : 23,
sex : "男"
//5 : true;
};
alert(person2.name + "==" + person2.age + "===" + person2.sex);
</script>
<body>
</body>
</html>
执行结果:


2、array类型
对JavaScript数据的各种操作
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<script type="text/javascript">
var colors = new Array(); //不限长度
var colors2 = new Array(20); //长度为20
var colors3 = new Array("red", "green", "blue"); //包含三项的数组
var colors4 = ["red", "blue", "green"]; //三个字符串的数组
var names = []; //空数组
//一下操作非不得已之下不要进行操作
var values = [1,2,]; //IE是3个数据一个undefined,其他的是两个数据
var options = [,,,,,,]; //别瞎几把搞,这个IE是7个其余的6个数据,其余的最后一个逗号之后不算
//数组的赋值
var colors5 = ["red", "blue", "green"]; //一个字符数组
alert(colors5[0]);
colors[2] = "black"; //修改第三项
colors[3] = "brown"; //可以为数组新增第4项
alert(colors5.length); //这个是4
alert(names.length); //0
//注意我们的length是可以改变的
colors5.length = 2;
alert(colors5[2]); //只要两项了,多余就变成了undefined了
colors5.length = 3;
alert(colors5[2]); //新添加一个第三项,但是没有赋值还是undefined
//在数组末尾添加数据,可以这样
colors5[colors5.length] = "black"; //数组最后添加一位数据
</script>
<body>
</body>
</html>
显示结果:





2.2 转化方法
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
var colors = ["red", "blue", "green"]; //创建一个包含3个字符串的数组
alert(colors.toString()); //显示字符串
alert(colors.valueOf());
alert(colors);
//使用join方法可以使用不同的分割符
alert(colors.join("$"));
alert(colors.join("||"));//默认是逗号隔开
</script>
</head>
<body>
</body>
</html>
结果显示:





2.3 栈方法
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
var colors = new Array(); //创建一个数组
//push返回修改之后数组的长度,pop返回弹出项
var count = colors.push("red", "green"); //推入两项数据
alert("push两个数据之后数组的长度" + count); //个数2个
count = colors.push("black"); //在添加一项数据
alert("push推入第三个数据之后:" + count); //第3个数据
var item = colors.pop(); //取得最后一项,然后弹出
alert("pop弹出的数据是:" + item);
alert("pop操作之后栈的长度为:" + colors.length);
</script>
</head>
<body>
</body>
</html>
结果:




2.4 重排序方法
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
//升序
function compare12(value1, value2)
{
if(value1 < value2)
{
return -1;
}
else if(value1 > value2)
{
return 1;
}
else
{
return 0;
}
}
//降序比较
function compare21(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(compare12);
alert(values);
values.sort(compare21);
alert(values);
</script>
</head>
<body>
</body>
</html>
结果:


---恢复内容结束---

浙公网安备 33010602011771号