5.1 Object類型html
//不指定大小
var colors1 = new Array();
//指定大小
var colors2 = new Array(20);
//指定數組項
var color3 = new Array('red', 'blue', 'green');
//省略new 操做符
var color4 = Array(''red', 'blue', 'green');
var colors1 = ['red', 'blue', 'green'];
//是喲那個字面量的BUG
//這樣會建立一個2(IE9+等)或3(IE8-)項的數組
var values = [1 ,2, ];
if(value instanceof Array){
//對數組進行某些操做
}
if(Array.isArray(value)){
//對數組執行某些操做
}
IE9+ | Firefox4+ | Safari5+ | Opera10.5+ | Chrome |
push() : 接受任意數量的參數,逐個添加到數組末尾,並返回修改後數組的長度
**param{any} 要添加到數組末尾的值或引用**...**return {Number} 修改後數組的長度
pop() : 從數組末尾移除最後一項,減小數組的length值,而後返回移除的項
**return {any} 移除的項
var colors = new Array(); //建立一個數組
var count = colors.push('red', 'green'); //推入兩項
alert(count); //2
count = colors.push(black); //推入另外一項
alert(count); //3
var item = colors.pop(); //取得最後一項
alert(item); //'black'
alert(colors.length); //2
var colors = new Array();
var count = colors.push('red', 'green'); //推入兩項
alert(count); //2
count = colors.push('black'); //推入另外一項
alert(count); //3
var item = colors.shift(); //取得第一項
alert(item); //'red'
alert(colors.length); //2
var colors = new Array();
var count = colors.unshift('red', 'green'); //推入兩項
alert(count); //2
count = colors.unshift('black'); //推入另外一項
alert(count); //3
var item = colors.pop(); //取得最後一項
alert(item); //'green'
alert(colors.length); //2
var values = [1, 2, 3, 4, 5];
values.reverse();
alert(values); //5,4,3,2,1
sort() : 接受一個比較函數函數來排序,默認按升序排列(調用每一個數組項的toString()轉型方法轉化爲字符串而後對字符串排序)
//1.無參數
var values = [0, 1, 5, 10, 15];
values.sort();
alert(values); //0, 1, 10, 15, 5
//2.傳入比較函數
/*
*比較函數接受兩個參數
*/
function compare(value1, value2){
//升序
if(value1 < value2){
return -1;
}else if(value1 > value2){
return 1;
}else{
return 0;
}
}
/*
function cpmpare(value1, value2){
return value1 - value2;
}
*/
var values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values); //0, 1, 5, 10, 15
var colors = ['red', 'green', 'blue'];
var colors2 = colors.concat('yellow', ['black', 'brown']);
alert(colors); //'red, green, blue'
alert(colors2); //'red, green, blue, black, brown'
var colors = ['red', 'green', 'blue', 'yellow', 'purple'];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);
alert(colors); //'red, green, blue, yellow, purple'
alert(colors2); //'green, blue, yellow, purple'
alert(colors3); //'green, blue, yellow'
var colors = ['red', 'green', 'blue'];
var removed = colors.splice(0, 1); //刪除第一項
alert(colors); //'green, blue'
alert(removed); //red, 返回數組中只包含一項
removed = colors.splice(1, 0, 'yellow', 'orange'); //從位置1開始插入兩項
alert(colors); //'green, yellow, orange, blue'
alert(removed); //[]
removed = colors.splice(1, 1, 'red', 'purple'); //插入兩項,刪除一項
alert(colors); //'green, red, purple, orange, blue‘
alert(removed); //yellow,返回的數組中只包含一項
IE9+ | Firefox2+ | Safari3+ | Opera9.5+ | Chrome |
IE9+ | Firefox2+ | Safari3+ | Opera9.5+ | Chrome |
IE9+ | Firefox2+ | Safari3+ | Opera9.5+ | Chrome |
var values = [1,2,3,4,5];
var sum = values.reduce(function(prev,cur,index,array){
return prev + cur;
});
alert(sum); //15
var values = [1,2,3,4,5];
var sum = values.reduceRight(function(prev,cur,index,array){
return prev + cur;
});
alert(sum); //15
第n個參數 | 表明 | 範圍 | 默認值 | 必須 |
1 | 年 | (無) | (無) | 是 |
2 | 月 | 0-11 | (無) | 是 |
3 | 日 | 1-31 | 1 | 否 |
4 | 時 | 0-23 | 0 | 否 |
5 | 分 | 0-59 | 0 | 否 |
6 | 秒 | 0-59 | 0 | 否 |
7 | 毫秒 | 0-999 | 0 | 否 |
var nowDate = new Date(); //當前時間
var specifyDate = new Date(1409540631971);
var parseDate = new Date('May 25, 2014'); //後臺自動調用Date.parse()將字符串轉爲毫秒數
var utcDate = new Date(2014, 4, 5, 17, 55, 55); //將根據本地時區轉換爲毫秒數(不一樣於Date.UTC(),後者參照GTC(格林尼治))
var someDate = new Date(Date.parse('May 25, 2004'));
日期格式 | 例子 | 兼容性 |
'月/日年' | '6/13/2004'; | |
'英文月 日, 年' | 'January 12, 2004'; | |
YYYY-MM-DDTHH:mm:ss:sssZ | 2004-05-25-T00:00:00 | ECMAScript 5 |
//GMT時間2000年1月1日午夜零時
var y2k = new Date(Date.UTC(2000,0));
//GMT時間2014年9月1日上午10:53
var y2k = new Date(Date.UTC(2014, 8, 1, 10, 53));
第n個參數 | 表明 | 範圍 | 默認值 | 必須 |
1 | 年 | (無) | (無) | 是 |
2 | 月 | 0-11 | (無) | 是 |
3 | 日 | 1-31 | 1 | 否 |
4 | 時 | 0-23 | 0 | 否 |
5 | 分 | 0-59 | 0 | 否 |
6 | 秒 | 0-59 | 0 | 否 |
7 | 毫秒 | 0-999 | 0 | 否 |
//取得開始時間
//var start = new Date().getTime();
//var start = +new Date();
var start = Date.now();
doSomething();
//取得中止時間
var stop = Date.now(),
result = stop - start;
IE9+ | Fire 3+ | Firefox 3+ | Safari3+ | Opera 10.5 | Chrome |
/*
* 特色
* 1.具備侷限性,不能定義在if、while、for、try-catch等結構中
* 2.能夠定義在被引用後(函數聲明提早)
*/
function sum(num1, num2){
return num1+num2;
}
/*
* 特色
* 1.能夠定義在任何位置
*/
var sum = function(num1, num2){
return num1 + num2;
};
/*
* 最後一個參數是函數體,前面的全部參數枚舉出新函數的參數
* 特色:
* 1.存在性能問題,須要解析兩次(第一次是解析常規ECMAScript代碼;第二次解析傳入構造函數的字符串)
* 2.執行環境爲window
* 3.能夠看做一種函數表達式
*/
var sum = new Function(num1, num2){
return num1 + num2;
};
/**
* 對數組對象進行排序的比較函數
* @param{string} propertyName 按照哪一個屬性來排序
* @return{function} 比較函數
*/
function createComparisonFunction(propertyName){
return function(obj1, obj2){
var value1 = obj1[propertyName];
var value2 = obj2[propertyName];
if(value1 < value2){
return -1;
}else if(value1 > value2){
reuturn 1;
}else{
return 0;
}
};
}
var data = [{name:'Zachary', age:28}, {name:'Nicholas', age:29}];
data.sort(createComparisonFunction('name');
alert(data[0].name); //Nicholas
data.sort(createComparisonFunction('age');
alert(data[0].name); //Zachary
function factorial(num){
if(num <= 1){
return 1;
}else{
return num * arguments.callee(num-1);
}
}
function outer(){
inner();
}
function(){
//嚴格模式下致使錯誤
alert(arguments.callee.caller);
}
outer();
function sayName(name){
alert(name);
}
function sum(num1, num2){
return num1 + num2;
}
function sayHi(){
alert('hi');
}
alert(sayName.length); //1
alert(sum.length); //2
alert(sayHi.length); //0
function sum(num1, num2){
return num1 + num2;
}
function callSum1(num1, num2){
return sum.apply(this, arguments); //傳入arguments對象數組(apply將其看成一個普通數組使用)
}
function callSum2(num1, num2){
return sum.apply(this, [num1, num2]); //傳入arguments對象
}
alert(callSum1(10, 10)); //20
alert(callSum2(10,10)); //20
function sum(num1, num2){
return num1 + num2;
}
function callSum(num1, num2){
return sum.call(this, num1, num2); //傳入arguments對象數組(apply將其看成一個普通數組使用)
}
alert(callSum1(10, )); //20
window.color = 'red';
var o = {color:'blue'};
function sayColor(){
alert(this.color);
}
var objectSayColor = sayColor.bind(o);
objectSayColor(); //blue
IE9+ | Firefox4+ | Safari 5.1+ | Opera 12+ | Chrome |
//狀況一:基本類型調用相應包裝類型的方法時自動封裝
var s1 = 'some text';
var s2 = s1.substring(2);
//狀況二:Object構造函數根據傳入的值得類型返回基本包裝類型實例
var obj = new Objetc('some text');
alert(obj instanceof String); //true
//狀況三:轉型函數,不一樣於使用new調用包裝類型的構造函數
var value = '25';
var number = Number(value);
alert(typeof number); //'number'
var falseObject = new Boolean(false);
var falseValue = false;
alert(typeof falseObjetc); //'objetc'
alert(typeof falseValue); //false
var num = 10.005;
alert(num.toFixed(2)); //'10.01'
alert(num.toExponential(2)); //'1.00e+1'
alert(num.toPrecision(2)); //'10'
var stringValue = 'hello world';
alert(stringValue.charAt(1)); //'e'
alert(stringVlaue.charCodeAt(1)); //'101'
alert(stringVlaue[1]);
var stringValue = 'hello';
var result = StringValue.concat('world', '!');
alert(result); //'hello world!'
alert(stringValue); //'hello'
//正值
var stringValue = 'hello world';
alert(stringVlaue.slice(3)); //'lo world'
alert(stringValue.substring(3)); //'lo world'
alert(stringValue.substr(3)); //'lo world'
//負值
alert(stringVlaue.slice(3, 7)); //'lo w'
alert(stringValue.substring(3, 7)); //'lo w'
alert(stringValue.substr(3, 7)); //'lo worl'
var stringValue = 'hello world';
alert(stringValue.indexOf('o', 6)); //7
alert(stringValue.lastIndexOf('0', 6)); //4
var stringValue = ' hello world ';
var trimmedStringVakue = stringValue.trim();
alert(stringValue); //' hello world '
alert(trimmedStringValue); //'hello world'
IE9+ | Firefox3.5+ | Safari5+ | Opera10.5+ | Chrome |
Firefox3.5+ | Safari5+ | Chrome8+ |
var text = 'cat, bat, sat, fat';
var pattern =/.at/;
//與pattern.exec(text)相同
var matches = text.match(pattern);
alert(matches.index); //0
alert(matches[0]); //'cat'
alert(pattern.lastIndex); //0
var text = 'cat, bat, sat, fat';
var pos = text.search(/at/);
alert(pos); //1
var text = 'cat, bat, satm fat';
var result = text.replace('at', 'ond');
alert(result); //'cond, bat, sat, fat'
result = text.replace(/at/g, 'ond');
alert(result); //'cond, bond, sond, fond'
var text = 'cat, bat ,sat, fat';
result = text.replace(/(.at)/g, 'word{$1}');
alert(result); //word(cat), word(bat) ,word(sat), word(fat)
function htmlEscape(text){
return text.replace(/[<>"&]/g, function(match, pos, originalText){
switch(match){
case '<':
return '$lt;';
case '>':
return '>';
case '&':
return '&';
case '\"':
return '"';
}
});
}
alert(htmlEscape('<p class="greeting">Hello world!</p>')); //<p class=$quot;greeting">Hello world!</p>
var colorText = 'red, blue, green , yellow';
var colors1 = colorText.split(','); //['red', 'blue', 'green', 'yellow']
var colors2 = colorText.split(',', 2); //['red', 'blue']
var colors3 = colorText.split(/[^\,] +/); //["", ",", ",", ",", ""]
var stringValue = ''yellow;
alert(stringValue.localeCompare('brick')); //1
alert(stringValue.localeCompare('yellow')); //0
alert(stringValue.localeCompare('zoo'));
alert(String.fromCharCode(104, 101, 108, 111)); //'hello'
var uri = 'http://www.wrox.com/illegal value.htm#start ';
//'http://www.wrox.com/illegal%20value.htm#start'
alert(encodeURI(uri));
//'http%3A%2F%2Fillegal%20value.htm%23start'
alert(encodeURIComponent(uri));
var uri = 'http%3A%2F%2Fillegal%20value.htm%23start';
//http%3A%2F%2Fillegal%20value.htm%23start
alert(decodeURI(uri));
//http://www.wrox.com/illegal value.htm#start
alert(decodeURIComponent(uri));
var msg = 'hello world';
eval('alert(msg);'); //'hello world'
//沒有給函數明確指定this值得狀況下,不管是經過將函數添加爲對象的方法
//仍是調用call()或apply(),this值等於Global對象
var global = function(){
return this;
}();
//參數中的最大值
var max = Math.max(3, 54, 32, 16);
alert(max); //54
var min = Math.min(3, 54, 32, 16);
alert(min); //3
//數組中的最大值
var values = [1, 2, 3, 4, 5, 6];
max = Math.max.apply(Math, values);
function selectFrom(lowerValue, upperValue){
var choices = upperValue - lowerValue + 1;
return Math.floor(Math.random() * choices + lowerValue);
}
var num = selectFrom(2,10);
alert(sum); //介於2和10之間(包含)的一個數