第五章
var person = new Object();
person.name = "Nicholas";
person.age = 29;
var person = {
name : "Nicholas",
age : 29
}
alert(person["name"]);
alert(person.name);
var propertyName = "name";
alert(person[propertyName]);
var colors = ["red","blue","green"];
alert(colors.length);
var colors = ["red","blue","green"];
colors.length = 2;
alert(colors[2]);
var person1 = {
toLocaleString : function () {
return "Nikolaos";
},
toString : function() {
return "Nicholas";
}
};
var person2 = {
toLocaleString : function () {
return "Grigorios";
},
toString : function() {
return "Greg";
}
};
var people = [person1, person2];
alert(people);
alert(people.toString());
alert(people.toLocaleString());
var colors = ["red", "green", "blue"];
alert(colors.join(","));
alert(colors.join("||"));
var colors = new Array();
var count = colors.push("red", "green");
alert(count);
count = colors.push("black");
alert(count);
var item = colors.pop();
alert(item);
alert(colors.length);
var colors = new Array();
var count = colors.push("red", "green");
alert(count);
count = colors.push("black");
alert(count);
var item = colors.shift();
alert(item);
alert(colors.length);
var colors = new Array();
var count = colors.unshift("red", "green");
alert(count);
count = colors.unshift("black");
alert(count);
var item = colors.pop();
alert(item);
alert(colors.length);
var values = [1, 2, 3, 4, 5];
values.reverse();
alert(values);
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);
function compare(value1, value2) {
return value2-value1;
}
var colors = ["red", "green", "blue"];
var colors2 = colors.concat("yellow", ["black", "brown"]);
alert(colors);
alert(colors2);
var colors = ["red", "green", "blue", "yellow", "purple"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);
alert(colors2);
alert(colors3);
var colors = ["red", "green", "blue"];
var removed = colors.splice(0,1);
alert(colors);
alert(removed);
removed = colors.splice(1, 0, "yellow", "orange");
alert(colors);
alert(removed);
removed = colors.splice(1, 1, "red", "purple");
alert(colors);
alert(removed);
var numbers = [1,2,3,4,5,4,3,2,1];
alert(numbers.indexOf(4));
alert(numbers.lastIndexOf(4));
alert(numbers.indexOf(4, 4));
alert(numbers.lastIndexOf(4, 4));
var person = { name: "Nicholas" };
var people = [{ name: "Nicholas" }];
var morePeople = [person];
alert(people.indexOf(person));
alert(morePeople.indexOf(person));
var numbers = [1,2,3,4,5,4,3,2,1];
var everyResult = numbers.every(function(item, index, array){
return (item > 2);
});
alert(everyResult);
var someResult = numbers.some(function(item, index, array){
return (item > 2);
});
alert(someResult);
var numbers = [1,2,3,4,5,4,3,2,1];
var filterResult = numbers.filter(function(item, index, array){
return (item > 2);
});
alert(filterResult);
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
return item * 2;
});
alert(mapResult);
var numbers = [1,2,3,4,5,4,3,2,1];
numbers.forEach(function(item,index,array){
});
var values = [1,2,3,4,5];
var sum = values.reduce(function(prev, cur, index, array){
return prev + cur;
});
alert(sum);
var numberObject = new Number(10);
var numberValue = 99;
alert(numberObject.toString());
alert(numberObject.toString(2));
alert(numberObject.toString(8));
alert(numberObject.toString(10));
alert(numberObject.toString(16));
alert(numberObject.toFixed(2));
numberObject = new Number(99);
alert(numberObject.toPrecision(1));
alert(numberObject.toPrecision(2));
alert(numberObject.toPrecision(3));
alert(typeof numberObject);
alert(typeof numberValue);
alert(numberObject instanceof Number);
alert(numberValue instanceof Number);
#slice()、substr()、substring()
var stringValue = "hello world";
alert(stringValue.slice(3));
alert(stringValue.substring(3));
alert(stringValue.substr(3));
alert(stringValue.slice(3, 7));
alert(stringValue.substring(3,7));
alert(stringValue.substr(3, 7));
alert(stringValue.slice(-3));
alert(stringValue.substring(-3));
alert(stringValue.substr(-3));
alert(stringValue.slice(3, -4));
alert(stringValue.substring(3, -4));
alert(stringValue.substr(3, -4));
#indexOf()和lastIndexOf()
var stringValue = "hello world";
alert(stringValue.indexOf("o"));
alert(stringValue.lastIndexOf("o"));
alert(stringValue.indexOf("o", 6));
alert(stringValue.lastIndexOf("o", 6));
var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");
while(pos > -1){
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);
}
alert(positions);
#trim()
var str=" Hello World ";
var trimmedStr=str.trim();
alert(str);
alert(trimmedStr);
#字符串大小寫轉換
var stringValue = "hello world";
alert(stringValue.toLocaleUpperCase());
alert(stringValue.toUpperCase());
alert(stringValue.toLocaleLowerCase());
alert(stringValue.toLowerCase());
#字符串的模式匹配
var text = "cat, bat, sat, fat";
var pattern = /.at/;
var matches = text.match(pattern);
alert(matches.index);
alert(matches[0]);
alert(pattern.lastIndex);
var pos = text.search(/at/);
alert(pos);
var result = text.replace("at", "ond");
alert(result);
result = text.replace(/at/g, "ond");
alert(result);
result = text.replace(/(.at)/g, "word ($1)");
alert(result);
function htmlEscape(text){
return text.replace(/[<>"&]/g, function(match, pos, originalText){
switch(match){
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case "\"":
return """;
}
});
}
alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");
var colors2 = colorText.split(",", 2);
var colors3 = colorText.split(/[^\,]+/);
#localeCompare()
var stringValue = "yellow";
alert(stringValue.localeCompare("brick"));
alert(stringValue.localeCompare("yellow"));
alert(stringValue.localeCompare("zoo"));
function determineOrder(value) {
var result = stringValue.localeCompare(value);
if (result < 0){
alert("The string 'yellow' comes before the string '" + value + "'.");
} else if (result > 0) {
alert("The string 'yellow' comes after the string '" + value + "'.");
} else {
alert("The string 'yellow' is equal to the string '" + value + "'.");
}
}
determineOrder("brick");
determineOrder("yellow");
determineOrder("zoo");