字符串扩展
1.是否有某个字符
let str = 'http://127.0.0.1:3000/index.html'; console.log(str.includes('index'));//true console.log(str.includes('logo'));//false
2.是否以某个字符开头
let str = 'http://127.0.0.1:3000/index.html'; console.log(str.startsWith('http://'));//true console.log(str.startsWith('login'));//false console.log(str.startsWith('http'));//true console.log(str.startsWith('h'));//true
3.是否以某个字符结尾
let str = 'http://127.0.0.1:3000/index.html';
console.log(str.endsWith('.html'));//true
console.log(str.endsWith('index.html'));//true
4.重复字符串
console.log('#'.repeat(30));
数值类型的扩展
1.判断数字是否是有限大
console.log(Number.isFinite(900000*90000000));//true console.log(Number.isFinite(1.7976931348623157e+309));//false console.log(Number.isFinite(Infinity));//false
2.数字的最大值
console.log(Number.MAX_VALUE);
3.判断是否为整数
console.log(Number.isInteger(0));//true console.log(Number.isInteger(0.2));//false
4. 将字符串转换为整型
console.log(Number.parseInt('123')) //123 console.log(Number.parseInt('123q')) //123 console.log(Number.parseInt('12a3')) //12 // 向下取整 console.log(Math.trunc(12.8));//12
对象的扩展
let arr1 = [1,2,3]; let arr2 = [1,2,3]; console.log(Object.is(arr1,arr2));//false let obj1 = {name : 'E'}; let obj2 = {name : 'E'}; console.log(Object.is(obj1,obj2));//false let obj3 = obj1; console.log(Object.is(obj3,obj1));//true let str1 = 'asd'; let str2 = 'asd'; console.log(Object.is(str1,str2));//true console.log(Object.is(NaN,NaN));//true
2.将一个对象的属性或者方法分配给另一个对象Object.Assign(newObj,oldObj)
置两个参数 第一个参数是被分配的对象 第二个参数是原对象
注意 如果说 被分配的对象有同名的key那么原来的数据会被覆盖
assign分配过去对象之后 新对象和原对象没有任何关系了
let newObj = {}; let oldObj = {}; newObj.name = 'li'; oldObj.name = 'zhang'; Object.assign(newObj,oldObj); console.log(newObj);//{ name: 'zhang' }
数组的扩展
1.Arrr.from 将伪数组对像或者可遍历的对象转换为真数组
let btns = document.getElementsByTagName('button'); let qbtns=document.querySelectorAll('button'); console.log(Object.prototype.toString.call(btns));//[object NodeList] console.log(Object.prototype.toString.call(qbtns));//[object NodeList] let arr = Array.from(btns); console.log(Object.prototype.toString.call(arr)); //[object Array]
2.Array.of 将一系列的值转换为数组
let arr = Array.of(1,3,5,7,9); console.log(arr);//[ 1, 3, 5, 7, 9 ] console.log(Object.prototype.toString.call(arr));//[object Array]
3.find 找出第一个满足条件的元素
find方法 查找数组中第一个为真的数据 filter
filter是返回所有为真的元素 返回值是一个数组 哪怕是一个元素 他也是数组
//let arr = ['Eric','Mary','Tom','Jerry','Jack']; //let result = arr.filter(element=>element.startsWith('M')); //let result1 = arr.filter(element=>element.endsWith('k')); //console.log(result);//[ 'Mary' ] //console.log(result1);//[ 'Jack' ] let result = arr.find(element=>element.startsWith('M')); let result1 = arr.find(element=>element.endsWith('k')); console.log(result);//Mary console.log(result1);//Jack
4.findIndex 找出第一个满足条件元素的索引
let arr = ['Eric','Mary','Tom','Jerry','Jack']; let result = arr.findIndex(element=>element.startsWith('J')); let result1 = arr.findIndex(element=>element.endsWith('y')); console.log(result);//3 console.log(result1);//1