这是我之前写的文章,若是错误,请指正,谢谢!数组
最近从新把红宝书拾起来复习了下,发现了好多遗忘的和遗漏的知识点,在这里总结一下我发现的本身忽略的知识点,但愿可以和你们一块儿进步:)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
复制代码
感谢阅读,我的成果,请勿转载:)