es6 numerical expansion, object method expansion (es6 study notes 06)

1. Numerical expansion of es6

Number.EPSILON: Is the minimum precision expressed by JavaScript. (When the difference between two numbers is less than it, the class considers the two numbers to be equal)

function equal(a,b){
    
    
            if(Math.abs(a-b) < Number.EPSILON){
    
    
                return true;
            }else{
    
    
                return false;
            }
        }
        console.log(equal(0.2+0.3,0.5))

二进制和八进制

let b=0b1100;
let o=0o666;

Number.isFinite: Check whether a value is a finite number

console.log(Number.isFinite(200))  //true
console.log(Number.isFinite(200/0));  //false
console.log(Number.isFinite(Infinity));  //false

Number.isNaN: Detect whether a number is NaN

console.log(Number.isNaN(321));  //false

Number.parseInt/Number.parseFloat: String to integer

console.log(Number.parseInt('5201314love'));  //5201314
console.log(Number.parseFloat('3.1415等等'));  //3.1415

Number.isInteger: Determine whether a number is an integer

console.log(Number.isInteger(88));  //true
console.log(Number.isInteger(3,12));  //false

Math.trunc: Erase the decimal part of the number

console.log(Math.trunc(2.88));  //2

Math.sign: Determine whether a number is positive or negative or zero

console.log(Math.sign(20));  //1
console.log(Math.sign(0));   //0
console.log(Math.sign(-90)); //-1

Two, es6 object method extension

Object.is: Determine whether the two values ​​are completely equal.

(Similar to ===, but in the judgment of NaN, the result is true)

console.log(Object.is(88,88));  //true
console.log(Object.is(NaN,NaN));  //true
console.log(NaN === NaN)  //false

Object.assign: Combination of objects

The second object overwrites the first one. When the properties in the object have the same name, they are overwritten, and they are merged when they have different names.

const hong = {
    
    
            name:'红红',
            age:19,
            like:'吃饭',
            text:'打豆豆'
}

const lv = {
    
    
            name:'绿绿',
            age:20,
            like:'喝水'
}
console.log(Object.assign(hong,lv));//第二个覆盖第一个

Object.setPrototypeOf/Object.getPrototypeof: Set prototype object/get prototype object.

object.setprototypeof takes the second parameter as a prototype and assigns it to the proto property of the first parameter.

const book = {
    
    
            name:'西游记'
}
const people = {
    
    
            renwu:['孙悟空','猪八戒','牛魔王']
}
Object.setPrototypeOf(book,people); //设置原型对象
console.log(Object.getPrototypeOf(book)); //获取原型对象
console.log(book);

Output result:

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48931875/article/details/113615919