這是我之前寫的文章,若是錯誤,請指正,謝謝!數組
最近從新把紅寶書拾起來複習了下,發現了好多遺忘的和遺漏的知識點,在這裏總結一下我發現的本身忽略的知識點,但願可以和你們一塊兒進步:)bash
對象類型的變量,在被轉爲數值類型時,例如Number(Obj)
或者進行對比判斷如==
的時候,會先調用對象的 valueOf 方法,若是 valueOf 方法不存在的話,則會調用 toString 方法,而後把調用獲得的結果轉換爲數值,舉個例子:函數
const obj = {
a: 1,
valueOf: function() {
return 2;
},
toString: function() {
return 3;
}
};
console.log(Number(obj)); // 2
console.log(obj == 2); // true
複製代碼
const obj = {
a: 1,
toString: function() {
return 3;
}
};
console.log(Number(obj)); // 3
console.log(obj == 3); // true
複製代碼
可是要注意的是,使用===
來判斷是沒法經過的,由於會判斷左端和右端的類型是否相同。性能
數組的length屬性表示數組的有多少項,這個屬性是能夠修改的,經過設置這個屬性可以在數組的末尾移出或者添加項,舉個例子:ui
const array = [1, 2, 3, 4, 5, 6];
console.log(array.length); // 6
array.length = 3;
console.log(array); // [1, 2, 3]
array.length = 5;
console.log(array); // [1, 2, 3, , ]
複製代碼
減小length能夠移除數組末尾的項,而增長 length 會在數組末尾添加空數組項。this
判斷一個數組是數組的方法:array instanceof Array
擴展:在ES6中,可使用Array.isArray(array)方法來判斷編碼
valueOf:返回數組自身; toString:spa
toLocaleString:數組的每一項調用 toLocaleString 方法,其餘的和 toSting 同樣 join:code
值得注意的是,若是數組的某一項若是是null或者undefined,那麼在調用上述的 toString、toLocaleString、join 方法時會被當總空數組項來處理。對象
array.splice(start, length, ...otherArguments)
刪除或者添加數組項
const array = ["a", "b", "c", "d", "e", "f"];
const newArr = array.splice(1, 2);
console.log(newArr); // ["b", "c"]
console.log(array); // ["a", "d", "e", "f"]
array.splice(2, 1, 2, ["a"]);
console.log(array); // ["a", "d", "e", 1, 2, ["a"], "f"]
複製代碼
"this is a string".subString()
爲例子:const str1 = String("this is a string"); // 建立一個String類型的實例
const str2 = str1.subString(); // 調用指定方法
str1 = null; // 銷燬實例
複製代碼
const str = Object("123");
console.log(str instanceof String); //true
const num = Object(123);
console.log(num instanceof Number); // true
複製代碼
const str = new String("str");
console.log(typeof str); // "object"
console.log(str instanceof String); // true
複製代碼
const str = new String("this is a string");
console.log(str.charAt(1)); // "h"
console.log(str.charAt("1")); // "h"
console.log(str.charAt("a")); // "t"
複製代碼
const str = new String("str");
console.log(str.concat("aaa", "bbb")); // "straaabbb"
console.log(str); // String {"str"}
複製代碼
slice、substring、substr:截取原有字符串的一部分,生成新的字符串;
slice:
substring:
substr:
indexOf 和 lastIndexOf 方法查找的時候使用的是 ===
,而且這兩個方法的第二個參數表示從字符串的第幾個位置開始查找;
indexOf:從前日後搜索,返回所傳參數在字符串中的第一個位置,不存在返回-1;
lastIndexOf:從後往前搜索,返回所傳參數在字符串中的第一個位置,不存在返回-1;
trim:刪除字符串兩端的空格,返回新的字符串;
localeCompare:比較兩個字符串在字母表中的位置,返回結果會隨着國家和地區變化;
fromChartCode:根據字符編碼返回字符串
const num = new Number(12345);
console.log(num.toExponential(2)); // 1.23e+4
複製代碼
感謝閱讀,我的成果,請勿轉載:)