《JavaScript高级程序设计2》学习笔记——引用类型

View Code
//Object类型
//new操作符创建Object实例
var person = new Object();
person.name
= 'chemdemo';
person.age
= 23;
//对象字面量创建对象
var person = {
name :
"chemdemo",
age :
23
};

var person = {
"name" : 'chemdemo',
'age' : 23
};

var person = {};
person.name
= 'chemdemo';
person.age
= 23;

function displayInfo(args) {
var output = "";
if (typeof args.name == "string") {
output
+= "Name: " + args.name + '\n';
}
if (typeof args.age == "number") {
output
+= "Age: " + args.age + '\n';
}
alert(output);
}
displayInfo ({
name :
'chemdemo',
age :
23
});
displayInfo ({
name :
"Greg"
});

alert(person[
"name"]);//访问person对象的name属性
alert(person.name);//访问person对象的name属性

var propertyName = "name";
alert(person[propertyName]);
//访问person对象的name属性

//Array类型
var colors = new Array();
var colors = new Array(20);//创建包含20个项的数组
var colors = new Array("red", "blue", "yellow");
var colors = Array(20);//创建包含20个项的数组

var colors = ["red", "blue", "yellow"];//创建一个包含3个字符串的数组
var name = [];//创建一个空数组
var values = [1, 2, ];//创建一个包含2或3项的数组(不建议这样)
var options = [,,,,];//创建一个包含4或5项的数组(不建议这样)

var colors = ["red", "blue", "yellow"];
alert(colors[
0]);//red
colors[2] = "black";//修改第三项值
colors[3] = "green";//新增第四项

var colors = ["red", "blue", "yellow"];
colors.length
= 4;
alert(colors[
3]);//undefined

var colors = ["red", "blue", "yellow"];
colors[colors.length]
= "black";//(在位置3)添加一种颜色
colors[colors.length] = "brown";//(在位置4)添加一种颜色

//转换方法
var colors = ["red", "blue", "yellow"];
alert(colors.toString());
//red,blue,yellow
alert(colors.valueOf());//red,blue,yellow
alert(colors);//red,blue,yellow

var colors = ["red", "blue", "yellow"];
alert(colors.join(
","));//red,blue,yellow
alert(colors.join("||"));//red||blue||yellow

//栈方法
var colors = new Array();
var count = colors.push("red", "black");//插入两项
alert(count);//2

count
= colors.push("blue");
alert(count);
//3

var item = colors.pop();
alert(item);
//blue
alert(colors.length);//2

//队列方法
var colors = new Array();
var count = colors.push("red", "black");//插入两项
alert(count);//2

count
= colors.push("blue");
alert(count);
//3

var item = colors.shift();//取得第一项
alert(item);//red
alert(colors.length);//2

var colors = new Array();
var count = colors.unshift("red", "black");
alert(count);
//2

//重排序方法
var values = [1, 2, 3, 4, 5];
values.reverse();
alert(values);
//5,4,3,2,1

var values = [0, 1, 5, 10, 15];
values.sort();
alert(values);
//0,1,10,15,5

function compare1(value1, value2) {//升序排列
if(value1 < value2) {
return -1;
}
else if(value1 > value2) {
return 1;
}
else {
return 0;
}
}
function compare2(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(compare1);
alert(values);
//0,1,5,10,15
values.sort(compare2);
alert(values);
//15,10,5,0

function compare1(value1, value2) {//降序排列
return value1 - value2;
}
function compare2(value1, value2) {//升序排列
return value2 - value1;
}

//操作方法
var colors = ["red", "blue", "yellow"];
var colors2 = colors.concat("green", ["black", "brown"]);
alert(colors.length);
//3
alert(colors2.length);//6

var colors = ["red", "blue", "yellow", "black", "brown", "green"];
var colors2 = colors.slice(3);
alert(colors2);
//black,brown,green
var colors3 = colors.slice(2, 4);
alert(colors2);
//yellow,black

var colors = ["red", "blue", "yellow"];
var removed = colors.splice(0, 1);//删除第一项
alert(colors);//blue,yellow
alert(removed);//red

removed
= colors.splice(1, 0, "green", "orange");//从位置1开始插入两项
alert(colors);//blue,green,orange,yellow
alert(removed);//空数组

removed
= colors.splice(1, 1, "red", "purple");//插入两项,删除一项
alert(colors);//blue,red,purple,orange,yellow
alert(removed);//green

//Date类型
var now = new Date();
var someDate = new Date(Date.parse("May 25, 2008"));
var someDate = new Date("May 25, 2008");

var y2k = new Date(Date.UTC(2000, 0));//GMT时间2000年1月1日午夜零时
var allFives = new Date(Date.UTC(2005, 4, 5, 17, 55, 55));//GMT时间2005年5月5日下午5:55:55

var y2k = new Date(2000, 0);//本地时间2000年1月1日午夜零时
var allFives = new Date(2005, 4, 5, 17, 55, 55);//本地时间2005年5月5日下午5:55:55

var date1 = new Date(2007, 0, 1);//January 1, 2007
var date2 = new Date(2007, 1, 1);//Februzary 1, 2007
alert(date1 < date2);//true


function createComparisionFunction(propertyName) {
return function(object1, object2) {
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if(object1 < object2) {
return -1;
}
else if(object1 > object2) {
return 1;
}
else {
return 0;
}
};
}

var data = [{name : "Zhang", age : 26}, {name : "Huang", age : 24}];
data.sort(createComparisionFunction(
"name"));
alert(data[
0].name);

function factorial(num) {//阶乘函数
if(num <= 1) {
return 1;
}
else {
return num*arguments.callee(num-1);
}
}
var res = facto'rial(5);
alert(res);

window.color = "red";
var o = {color: "blue"};

function sayColor() {
alert(this.color);
}

sayColor();//red
o.sayColor = sayColor;
o.sayColor();

function sum(num1, num2) {
return num1+num2;
}

function callSum1(num1, num2) {
return sum.apply(this, arguments);
}
alert(callSum1(10, 20));

function callSum2(num1, num2) {
return sum.apply(this, [num1, num2]);
}
alert(callSum2(10, 20));

var stringValue = "Lorem ipsum dolor sit amet, consecteurt adipisicing elit";
var pos = stringValue.indexOf("e");
var positions = new Array();
while(pos > -1) {
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);
}
alert(positions);

var text =
'cat, bat, sat, fat';
var pattern = /.at/;
var matchs = text.match(pattern);
alert(matchs.index);//0
alert(matchs[0]);//cat
alert(pattern.lastIndex);//0

function htmlEscape(text) {
return text.replace(/[<>\"&]/g, function(match, pos, originanText) {
switch(match) {
case "<" : return "&lt;";
case ">" : return "&gt;";
case "&" : return "&amp;";
case "\"" : return "&quto;";
}
});
}

var txt = "<p class=\"greeting\">Hello World!</p>";
alert(htmlEscape(txt));

var text = "red, blue, green, yellpw";
alert(text.split(/[^\,]+/));

var url =
'http://ce.sysu.edu.cn/hope/#-&424 2';
alert(encodeURI(url));
alert(decodeURI(encodeURI(url)));
alert(encodeURIComponent(url));
alert(decodeURIComponent(encodeURIComponent(url)));

function selectFrom(lowerValue, upperValue) {
var dis = upperValue - lowerValue;
return Math.floor(Math.random() * dis + lowerValue);
}

var colors = ["red", 'blue', 'yellow', 'brown', 'black', 'green'];
alert(colors[selectFrom(
0, colors.length - 1)]);
posted @ 2011-02-26 16:48  chemdemo  阅读(236)  评论(0编辑  收藏  举报