經常使用Object的屬性和方法
建立空Object
var o1 = {};
var o2 = Object.create(Object.prototype);
建立Object
var o3 = {
str: "string...",
fun: function() {
return this.str;
}
};
給對象添加屬性
Object.defineProperty(o1, "newPropertyName", { value: "str", enumerable: true, writable: true });
給對象添加多個屬性
Object.defineProperties(o1, {
newPropertyName1: { value: "str1", writable: false },
newPropertyName2: { value: "str2", writable: true }
});
給對象添加getter
Object.defineProperty(o1, "logIt", {
get: function () {
return this.newPropertyName;
}
});
給對象添加setter
Object.defineProperty(o1, "renew", {
set: function (newValue) {
this.newPropertyName = newValue;
}
});
返回屬性配置(對象)
Object.getOwnPropertyDescriptor(o1, "newPropertyName");
返回全部屬性名(數組)
Object.getOwnPropertyNames(o1);
返回對象原型(對象)
Object.getPrototypeOf(o1);
阻止對象擴展
Object.preventExtensions(o1);
凍結對象
Object.freeze(o1);
密封對象
Object.seal(o1);
檢測是否可擴展(布爾值)
Object.isExtensible(o1);
檢測是否凍結(布爾值)
Object.isFrozen(o1);
檢測是否密封(布爾值)
Object.isSealed(o1);
對象原型對象(對象實例)的屬性和方法
構造函數
o1.constructor;
檢測非繼承屬性是否存在(布爾值)
o1.hasOwnProperty("newPropertyName");
檢測是否爲實例原型(布爾值)
o1.isPrototypeOf(o1);
檢測屬性是否可遍歷
o1.propertyIsEnumerable("newPropertyName");
返回對象字符串表示
o1.toString();
返回對象原始值
o1.valueOf();
經常使用Array屬性和方法
建立數組
var arr = [1,2,3,4,5];
構造函數長度
Array.length;
數組對象附加屬性
Array.prototype;
檢測是否爲Array
Array.isArray();
數組實例的屬性和方法
構造函數
arr.constructor;
返回長度
arr.length;
Mutator方法
如下方法改變原數組
增長或刪除一個數組元素
arr.pop();
arr.push("end");
arr.shift();
arr.unshift("start");
添加或刪除任意一個元素
arr.splice(1, 3); //從第二個開始,刪除三個元素
arr.splice(1, 0, "newing","anotherNewing"); //從第二個開始,刪除零個,添加兩個新元素
顛倒數組元素
arr.reverse();
排列數組元素
arr.sort();
Accessor方法
如下方法不改變原數組
組合成新數組(Array)
var newArr = arr.concat("newArrayItem");
抽取元素組成新數組(Array)
var newArr = arr.slice(1, 3); //抽取從第二個元素開始到第四個元素(不包括第四個)結束成新數組
鏈接數組並以某種符號分割組合成字符串(String)
var string = arr.join("-");
鏈接數組並組合成字符串(String)
var string = arr.toString();
查詢匹配元素返回索引值(number)
var index = arr.indexOf("end");
從後往前查詢匹配元素返回索引值(number)
var index = arr.indexOf("end");
Accessor方法
如下方法不改變原數組
每一個元素執行一次回調函數
arr.forEach(function (currentValue, index, array) {
//body...
});
全部元素是否都經過函數測試(Boolean)
var result = arr.every(function (currentValue, index, array) {
return typeof currentValue === "string";
});
是否至少一個元素經過函數測試(Boolean)
var result = arr.some(function (currentValue, index, array) {
return currentValue.length === 3;
});
返回包括函數測試爲true的元素新數組(array)
var newArray = arr.filter(function (currentValue, index, array) {
return currentValue.length === 3;
});
返回回調函數返回值組成的新數組(array)
var newArray = arr.map(function (currentValue, index, array) {
return currentValue + "[string...]";
});
reduce
var result = arr.reduce(function (previousValue, currentValue, index, array) {
return previousValue + " " + currentValue;
}, "beginDoingThis"); //第二參數爲初始值
reduceRight
var result = arr.reduceRight(function (previousValue, currentValue, index, array) {
return previousValue + " " + currentValue;
}, "beginDoingThis"); //第二參數爲初始值
經常使用Function的屬性和方法
獲取參數(like array)
function add(x, y) {
return arguments;
}
獲取函數指望接受參數個數(number)
add.length;
獲取函數名(string)
add.name;
call方法
add.call(null, 1, 2, 3);
apply方法
add.apply(null, [1, 2, 3]);
bind方法
var anotherAdd = add.bind(null, 1, 2);
返回函數源碼
add.toString();
經常使用String屬性和方法
建立字符串
var s1 = "string...";
var s2 = String("hello there good morning...");
添加附加屬性和方法
String.prototype
Unicode轉爲string(string)
var str = String.fromCharCode(65, 66, 67);
索引上string轉爲Unicode見實例中的方法(number)
var uniq = String.prototype.charCodeAt.call(s1, 0);
String實例的屬性和方法
構造函數
s1.constructor;
字符串長度(number)
s1.length;
刪除兩端的空白字符
s1.trim();
string和unicode轉化和索引
索引取值(string)
s1.charAt("0");
string索引上值轉爲unicode(unmber)
s1.charCodeAt(0);
查找字符在字符串中的索引(number)
s1.indexOf("searchString", 0); //第二個參數爲開始的位置
從後往前查找字符在字符串中的索引(number)
s1.lastIndexOf("searchString", 0); //第二個參數爲開始的位置
建立子字符串或數組的方法
提取字符串返回新字符串(string)
var str = s1.substring(-3,s1.length); //接收正整數(負數轉化爲0)
提取字符串返回新字符串(string)
var str = s1.substr(3,2); //可接收負數(從右往左),第二個參數是指望字符串長度
提取字符串返回新字符串(string)
var str = s1.slice(-3, 10); //可接收負數(length-數值)
切割字符串返回數組(array)
var arr = s2.split(" ", 2); //以空格爲分隔符最多切割兩次
正則相關的方法
正則提取匹配項返回數組(array or null)
var arr = s1.match(/str/g);
正則替換匹配項返回新字符串(string)
var newstr = s1.replace(//.{3}/, "!!!");
正則返回首次匹配項的索引(number)
var index = s1.search(/str/g);
經常使用RegExp屬性和方法
建立RegExp
var r1 = new RegExp(/hello/ig);
var r2 = //w{10}/;
爲正則添加對象
RegExp.prototype
length(number===2)
RegExp.length
RegExp實例的屬性和方法
構造函數
r1.constructor;
是否開啓global、ignoreCase和multiline(boolean)
r1.global
r1.ignoreCase
r1.multiline
匹配的最後位置(number)
r1.lastIndex;
設置最後匹配位置(與exec配合)
r1.lastIndex = 0;
執行匹配操做(array)
r1.exec("hello there, hello world");
測試是否匹配(boolean)
r1.test("hello there");