es6的Math、Number、String 、Array、Boject 的新 API以及Symbol
接下来就为大家一一介绍和举例说明一下Math、Number、String 、Array、Boject 的新 API以及Symbol
//方便用于去除一个数的小数部分,返回整数部分
//Math
let a = Math.trunc(4.1)
console.log(a);//打印出的结果是:4
//Math.sign()用来判断一个数到底是整数,负数,还是0
let b = Math.sign(2);
console.log(b);//打印出的结果是:1
let c = Math.sign(-2);
console.log(c);//打印出的结果是:-1
let d = Math.sign(0);
console.log(d);//打印出的结果是:0
//Math.sqrt()方法用于计算一个数的平方根
let ping = Math.sqrt(4);
console.log(ping);//打印出的结果是:2
//Math.cbrt()方法用于计算一个数的立方根
let li = Math.cbrt(27);
console.log(li);//打印出的结果是:3
//Number
console.log(Number.isInteger(12));//返回值是true
console.log(Number.isInteger(12.0));//返回值是true
console.log(Number.isInteger(12.1));//返回值是flase
var number = '123s';
var reg = /^[0-9\.]+$/;
console.log(reg.test(number));//返回值是flase
console.log(Number.isNaN(12));//返回值是flase
//String
var jj = "hello";
console.log(jj.includes("e"));//返回值是true
console.log(jj.startsWith("h"));//返回值是true
console.log(jj.endsWith("lo"));//返回值是true
console.log("234".repeat(100));//重复100个234
//Array
let arr = [1,4,5,7];
let arr1 = Array.from(arr);
console.log(arr1);//打印出[1, 4, 5, 7]
console.log(Array.of(1,2,3,4));//类似new Array 打印出[1, 2, 3, 4]
console.log([0,0,0,2,3,4,5].fill('sdf',2));//打印出 [0, 0, "sdf", "sdf", "sdf", "sdf", "sdf"]
//Object
//Object.assign(obj,obj1,obj2,....)浅拷贝
//1,合并多个对象
//2,克隆对象(浅);
//3,为对象添加多个方法
let obj = {x:1,y:2};
let obj1 = {c:3,d:7};
let obj2 = Object.assign({},obj,obj1);
console.log(obj);//打印出 {x: 1, y: 2}
console.log(obj1);//打印出{c: 3, d: 7}
console.log(obj2);//打印出{x: 1, y: 2, c: 3, d: 7}
var obj4 = {a:1,b:2};
Object.assign(obj4,{
say:() => {},
run:() => {},
eat:() => {}
});
let per = {
name:'',
sex:0,
age:23
}
let ren = {
//__proto__:per,
xh:'123',
banji:"师徒"
};
Object.setPrototypeOf(ren,per);
console.log(per.age);//打印出23
//Symbol
let m = 1;
let l = 1;
console.log(m==l);//打印出true
let mm = Symbol();
let ll = Symbol();
console.log(mm==ll);//打印出flase
//这里Symbol前面不能使用new操作符
//阻止对象的属性被篡改
每个类型里都会有很多方法,我写的这几种只是我会在工作中偶然会遇到,希望能帮到大家