【前端】简约代码案例——笔记1

56 阅读1分钟

1、if...else...与三元表达式

let i =true
let n=i?true:false

等价于

    let i = true
    let n = ""
    if (i) {
        n = true
    } else {
        n = false
    }
    console.log(n);

2、parseInt()与+

let n="1"
console.log(typeof(+n));//number

等价于

    let n="1"
    console.log(typeof(parseInt(n)));//number

3、String()/toString()与""+

let n=1
console.log(typeof(""+n));//string

等价于

    let n=1
    console.log(typeof(String(n)));//string
    console.log(typeof(n.toString()));//string

4、合并两个对象Object.assign与扩展运算

    let obj1 = { nameOne: "one" }
    let obj2 = { nameTwo: "two" }
    console.log({...obj1,...obj2});//Object { nameOne: "one", nameTwo: "two" }

等价于

    let obj1 = { nameOne: "one" }
    let obj2 = { nameTwo: "two" }
    console.log(Object.assign(obj1,obj2));//Object { nameOne: "one", nameTwo: "two" }