引用類型的值(對象)是引用類型的一個實例。javascript
建立新對象:new 操做符後跟一個 構造函數前端
var person = new Object();
上面:java
Object
Object()
var person = new Object();
var person = { name: "Nicholas", // 屬性之間用逗號隔開 age: 29 // 在最後一個屬性後面不加都好 }
var person = { "name": "Nicholas", // 屬性之間用逗號隔開 "age": 29 // 在最後一個屬性後面不加都好 5: true // 數值屬性會自動轉換爲字符串 }
alert(person["name"]); //要將屬性以字符串形式放在方括號中 alert(person.name);
var propertyName = "name"; alert(person[propertyName]);
ECMAScript 數組的每一項能夠保存任何類型的數據。好比:第一個位置保存字符串,第二個位置保存數值,第三個位置保存對象,等。數組大小是能夠動態調整的。
var colors = new Array(); var colors = new Array(20); //長度爲20 var colors = new Array("red", "blue", "green"); //建立了一個包含3個字符串的數組
var colors = Array(3); var colors = Array("greg");
var colors = ["red", "blue", "green"];// 建立一個包含3個字符串的數組 var names = []; // 建立一個空數組 var values = [1, 2, ]; // 不要這樣! 這樣會建立一個包含 2 或 3 項的數組 var options = [, , , , ,] // 不要這樣! 這樣會建立一個包含 5 或 6 項的數組
ES5新增 Array.isArray()
方法,肯定某個值是否是數組。 有些瀏覽器不支持。正則表達式
if (Array.isArray(value)){ //執行某些操做 }
toString()
, valueOf()
express
var colors = ["red", "blue", "green"]; alert(colors.toString()); // "red,blue,green" 返回數組中每一個值的字符串以逗號分隔的拼接字符串 alert(colors.valueOf()); // ["red", "blue", "green"] 返回的仍是數組 alert(colors); // 與 toString() 相同
join()
數組
var colors = ["red", "blue", "green"]; console.log(colors.join(",")); // red,blue,green console.log(colors.join("||")); // red||blue||green
push()
接收任意數量的參數,把他們逐個添加到數組末尾瀏覽器
pop()
從數組末尾移除最後一項,減小數組的 length 值, 返回 移除的項。app
var colors = new Array(); // 建立一個數組 var count = colors.push("red", "blue"); // 推入兩項 console.log(count); // 2 count = colors.push("black"); // 推入另外一項 console.log(count); // 3 var item = colors.pop(); // 取得最後一項 console.log(item); // black console.log(colors.length); // 2
shift()
移除第一項dom
unshift()
在數組前端添加項函數
reverse()
反轉數組項的順序
sort()
按升序排列數組項,sort()
會調用 toString()
轉型方法,
==特別注意==:因此sort()
比較的是字符串。
var values = [0, 1, 10, 15, 5]; console.log(values.reverse()); // [5, 15, 10, 1, 0] console.log(values.sort()); // [0, 1, 10, 15, 5]
將比較函數傳遞到 sort()
能夠保持正確的升序:
function compare(value1, value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } } var values = [0, 1, 10, 15, 5]; values.sort(compare); console.log(values); // [0, 1, 5, 10, 15]
用 ES6中的箭頭函數來寫數組升序:
var values = [0, 1, 10, 15, 5]; values.sort((value1, value2) => value1 - value2); console.log(values); // [0, 1, 5, 10, 15]
用 ES6中的箭頭函數來寫數組降序:
var values = [0, 1, 10, 15, 5]; values.sort((value1, value2) => value2 - value1); console.log(values); // [15, 10, 5, 1, 0]
concat()
建立副本,將參數添加到副本的末尾,返回新構建的數組
var colors = ["red", "blue", "green"]; colors2 = colors.concat("yellow", ["black", "brown"]); console.log(colors); // ["red", "blue", "green"] console.log(colors2); // ["red", "blue", "green", "yellow", "black", "brown"]
slice()
數組截取
var colors = ["red", "blue", "green", "yellow", "purple"]; var colors2 = colors.slice(1); // 第一項 到 末尾 var colors3 = colors.slice(1, 4); // 第一項 到 第四項 console.log(colors2); // ["blue", "green", "yellow", "purple"] console.log(colors3); // ["blue", "green", "yellow"]
splice()
var colors = ["red", "green", "blue"]; var removed = colors.splice(0,1); //remove the first item alert(colors); //green,blue alert(removed); //red - one item array removed = colors.splice(1, 0, "yellow", "orange"); //insert two items at position 1 alert(colors); //green,yellow,orange,blue alert(removed); //empty array removed = colors.splice(1, 1, "red", "purple"); //insert two values, remove one alert(colors); //green,red,purple,orange,blue alert(removed); //yellow - one item array
indexOf()
要查找的值 從前日後 首次出現的索引
lastIndexOf()
要查找的值 從後往前 首次出現的索引
var numbers = [1,2,3,4,5,4,3,2,1]; alert(numbers.indexOf(4)); //3 alert(numbers.lastIndexOf(4)); //5 alert(numbers.indexOf(4, 4)); //5 alert(numbers.lastIndexOf(4, 4)); //3 var person = { name: "Nicholas" }; var people = [{ name: "Nicholas" }]; var morePeople = [person]; alert(people.indexOf(person)); //-1 alert(morePeople.indexOf(person)); //0
every()
對數組中每一項運行給定函數,每一項都true,則返回 true
some()
var numbers = [1,2,3,4,5,4,3,2,1]; var everyResult = numbers.every(function(item, index, array){ return (item > 2); }); alert(everyResult); //false var someResult = numbers.some(function(item, index, array){ return (item > 2); }); alert(someResult); //true
filter()
var numbers = [1,2,3,4,5,4,3,2,1]; var filterResult = numbers.filter(function(item, index, array){ return (item > 2); }); alert(filterResult); //[3,4,5,4,3]
forEach()
對每一項運行指定函數,無返回值
map()
var numbers = [1,2,3,4,5,4,3,2,1]; var mapResult = numbers.map(function(item, index, array){ return item * 2; }); alert(mapResult); //[2,4,6,8,10,8,6,4,2]
reduce()
從前日後執行 函數
reduceRight()
從後往前執行 函數
var values = [1,2,3,4,5]; var sum = values.reduce(function(prev, cur, index, array){ return prev + cur; }); alert(sum); //15
ECMAScript 中的 Date 類型是在早期 Java 中的 java.util.Date 類基礎上構建的,使用自UTC(國際協調時間)1970年1月1日午夜(零時)開始通過的毫秒數來保存日期。Date 類型保存的日期可以精確到1970年1月1日以前或以後的100 000 000 年。
構造函數建立:
var now = new Date();
var now = new Date(); alert(now); //Wed Mar 20 2019 21:32:55 GMT+0800 (中國標準時間) 當前時間 var someDate = new Date(Date.parse("May 25, 2004")); alert(someDate); //Tue May 25 2004 00:00:00 GMT+0800 (中國標準時間)
ES5 中添加了 Date.now()
方法
// 取得開始時間 var start = Date.now(); // 調用函數 doSomething(); // 取得中止時間 var stop = Date.now(); var result = stop - start;
對於不支持 Date.now()
的瀏覽器,使用 +
操做符也能夠獲取 Date 對象的時間戳:
// 取得開始時間 var start = +new Date(); // 調用函數 doSomething(); // 取得中止時間 var stop = +new Date(); var result = stop - start;
var expression = / pattern / flags; / 模式(pattern) / 標誌(flags) 的組合
3種標誌:
模式中使用的全部元字符都必須轉義。正則表達式中的元字符包括:
( [ \ ^ $ | ) ? * + . ] }
// 匹配字符串中全部"at"的實例 var pattern1 = /at/g; // 匹配第一個"bat" 或 "cat",不區分大小寫 var pattern2 = /[bc]at/i; // 匹配第一個"[bc]at",不區分大小寫 var pattern3 = /\[bc\]at/i; // 匹配全部以 "at" 結尾的3個字符的組合,不區分大小寫 var pattern4 = /.at/gi; // 匹配全部 ".at",不區分大小寫 var pattern5 = /\.at/gi;
/* * 匹配第一個"bat" 或 "cat",不區分大小寫 */ var pattern = new RegExp("[bc]at", "i");
exec()
捕獲組
test()
驗證。經常使用於 驗證用戶輸入的狀況。
ECMAScript 中,函數其實是對象。每一個函數都是 Function 類型的實例,與其餘引用類型同樣具備屬性和方法。函數是對象,函數名是一個指向對象的指針,不會與某個函數綁定。
下面的代碼能夠正常運行:
alert(sum(10,10)); //20 function sum(num1, num2){ return num1 + num2; }
將函數聲明改成函數表達式會錯誤:
alert(sum(10, 20)); var sum = function(num1, num2){ retuen num1 + num2; };
arguments
this
this 引用的是函數執行的環境對象——或者也能夠說是 this 值(當在網頁的全局做用域中調用函數時, this 對象引動的就是 window)。
每一個函數都包含兩個屬性
length
prototype
每一個函數都包含兩個非繼承而來的方法
apply()
call()
ES5還定義了一個方法
bind()
Boolean
、Number
,String
引用類型與基本包裝類型的區別:
布爾對象:
var falseObject = new Boolean(false); var result = falseObject && true; alert(result); //true
var falseValue = false; result = falseValue && true; alert(result); //false
var numberObject = new Number(10);
toString()
參數表示返回幾進制數值的字符串形式alert(numberObject.toString()); //"10" alert(numberObject.toString(2)); //"1010" alert(numberObject.toString(8)); //"12" alert(numberObject.toString(10)); //"10" alert(numberObject.toString(16)); //"a"
toFixed()
參數表示顯示幾位小數alert(numberObject.toFixed(2)); //outputs "10.00"
toExponential()
返回以指數表示法(也稱 e 表示法)表示的數值的字符串形式var num = 10; alert(num.toExponential(1)); // "1.0e+1"
toPrecision()
參數表示數值的全部數字的位數var numberObject = new Number(99); alert(numberObject.toPrecision(1)); //"1e+2" alert(numberObject.toPrecision(2)); //"99" alert(numberObject.toPrecision(3)); //"99.0"
var stringObject = new String("hello world");
charAt()
參數:字符位置 返回:給定位置的字符(單字符字符串形式)var stringValue = "hello world"; alert(stringValue.charAt(1)); // "e"
charCodeAt()
參數:字符位置 返回:給定位置字符的編碼var stringValue = "hello world"; alert(stringValue.charCodeAt(1)); // 輸出 「101」,也就是小寫字母 "e" 的字符編碼
[]
ES5中的訪問個別自負的方法var stringValue = "hello world"; alert(stringValue[1]); // "e"
concat()
字符串拼接var stringValue = "hello"; var result = stringValue.concat("world"); alert(stringValue); // "hello" alert(result); // "hello world"
slice()
substr()
substring()
var stringValue = "hello world"; alert(stringValue.slice(3)); //"lo world" alert(stringValue.substring(3)); //"lo world" alert(stringValue.substr(3)); //"lo world" alert(stringValue.slice(3, 7)); //"lo w" alert(stringValue.substring(3,7)); //"lo w" alert(stringValue.substr(3, 7)); //"lo worl" 第二個參數指定的是要返回的字符個數
indexOf()
從指定位置向後搜索lastIndexOf()
從指定位置向前搜索var stringValue = "hello world"; alert(stringValue.indexOf("o")); //4 alert(stringValue.lastIndexOf("o")); //7 alert(stringValue.indexOf("o", 6)); //7 從第6個開始向後搜索 alert(stringValue.lastIndexOf("o", 6)); //4 從第6個開始向前搜索
trim()
方法ECMAScript 中定義的 trim()
方法。
建立一個字符串的副本,刪除前置及後綴的全部空格,而後返回結果。
toLowerCase()
toLocaleLowerCase()
toUpperCase()
toLocaleUpperCase()
var stringValue = "hello world"; alert(stringValue.toLocaleUpperCase()); //"HELLO WORLD" alert(stringValue.toUpperCase()); //"HELLO WORLD" alert(stringValue.toLocaleLowerCase()); //"hello world" alert(stringValue.toLowerCase()); //"hello world"
mach()
只接受一個參數var text = "cat, bat, sat, fat"; var pattern = /.at/; var matches = text.match(pattern); alert(matches.index); //0 alert(matches[0]); //"cat" alert(pattern.lastIndex); //0
search()
接受惟一參數,返回字符串中第一個匹配項的索引var text = "cat, bat, sat, fat"; var pos = text.search(/at/); alert(pos); //1
replace()
l兩個參數:參數一:RegExp對象或者一個字符串,參數二:字符串或函數。var text = "cat, bat, sat, 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" result = text.replace(/(.at)/g, "word ($1)"); alert(result); //word (cat), word (bat), word (sat), word (fat)
split()
分割 結果是數組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(/[^\,]+/); //["", ",", ",", ",", ""]
localCompare()
方法localCompare()
比較兩個字符串在字母表中的順序var stringValue = "yellow"; alert(stringValue.localeCompare("brick")); //1 alert(stringValue.localeCompare("yellow")); //0 alert(stringValue.localeCompare("zoo")); //-1
fromCharCode()
方法fromCharCode()
接收一或多個字符串編碼,把他們轉換成一個字符串alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"
內置對象的定義:由 ECMAScript 實現提供的、 不依賴於宿主環境的對象,這些對象在 ECMAScript 程序執行以前就已經存在了。
encodeURI()
encodeURIComponent()
var uri = "http://www.wrox.com/illegal value.htm#start"; //"http://www.wrox.com/illegal%20value.htm#start" alert(encodeURI(uri)); //"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start" alert(encodeURIComponent(uri));
decodeURI()
decodeURIComponent()
var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start"; //http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start alert(decodeURI(uri)); //http://www.wrox.com/illegal value.htm#start alert(decodeURIComponent(uri));
eval()
方法eval()
方法就像是一個完整的 ECMAScript 解析器,它只接受一個參數,即要執行的 ECMAScript 字符串。
eval("alert('hi')"); // 等價於下面 alert("hi");
Math.max()
Math.min()
Math.ceil()
向上舍入Math.floor()
向下舍入Math.round()
四捨五入Math.random()
返回大於等於 0 小於 1 的隨機數