首页>>新闻中心>>ES6学习方法

箭头函数和深度拷贝

来源: 本站    发布时间: 2021-02-22 15:03    阅读次数:

// 箭头函数
    // this指向定义时所在的对象,而不是调用时所在的对象
    // 不可以作为构造函数
    // 不可以使用arguments对象
    function sum(x, y) {
        return x + y
    }
    console.log(sum(3, 5))
    let sum = function (x, y) {
        return  x + y
    }
    let sum = (x, y) => {
        console.log(this) // 这里的this就是window
        return x + y
    }
    console.log(sum(1, 2))
    let name = "Yang"
    let age = 19
    let s = "school"
    let obj = {
       name,
       age,
       [s]: "阆中中学", //属性名是一个变量需要用[]来表示
        study() {
            console.log(this)
            console.log(this.name + "正在学习")
        }
    }
    obj.study()
    console.log(Object.is(NaN,NaN)) // true
    console.log(Object.is(-1, 1))

    let obj1 = {
        name: "Yang",
        age : 19
    }
    let obj2 = {
        name: "Yang",
        age : 19
    }
    console.log(Object.is(obj1, obj2)) //这里面不相等,主要是值相等,但是地址不相等。js中对象的是引用数据类型。
    let x = {
        a:3,
        b:4
    }
    console.log({...x}) //重点
    let y = {...x} //对象的扩展运算符
    console.log(y)
    console.log(Object.is(x, y))
    let z = {
        c: 7,
        a: 6
    }
    Object.assign(x,z) //对象的合并
    console.log(x)
    console.log("a" in x) //true a这个属性值在x里面
    let  arr = [1, 2, 3]
    console.log(3 in arr) //false 这里面是判断小标为3的里面有没有值
    let obj3 = {
        name: "Yang",
        age : 19,
        [s] : "四川师范大学成都学院-四川工商学院"
    }
    for(let key in obj3){
        console.log(key) // 属性
        console.log(obj3[key]) //值
    }
    Object.keys(obj3).forEach((item,index) => {
        console.log(item) //name age school
        console.log(index) //0, 1, 3
    })
    Object.getOwnPropertyNames(obj3).forEach(key => {
        console.log(key, obj3[key])
    })
    Reflect.ownKeys(obj3).forEach(key => {
        console.log(key, obj3[key])
    })

    let target = {
        a: {
            b: {
                c: 1,
                d: 2
            },
            e: 2,
            f: 3,
            g: 4
        }
    }
    let source = {
        a: {
            b: {
                c: 1
            },
            e: 2,
            f: 3
        }
    }
    Object.assign(target, source)
    console.log(target)
    let a = 5
    let b = a
    a = 6
    console.log(a, b) //这是深拷贝

    let obja = {
        name: "严钢",
        age: 18
    }
    obja.age = 24
    // let objb = obja //这里面就是浅拷贝 这里面 obja  objb指向同一个内存地址
    //console.log(objb)
    console.log(obja)
    // JSON.stringify() //把一个对象转化为JSON字符串
    // JSON.parse() //把一个JSON字符串转换为一个对象
    let str =  JSON.stringify(obja)
    let objb = JSON.parse(str)
    console.log(objb)
    let checkType = data => {
        return Object.prototype.toString.call(data).slice(8, -1) //判断类型
    }

    let deepClone = target =>{
        let targetType = checkType(target);
        let result
        if ( targetType === "Object"){
            result = {}
        }else if(targetType === "Array"){
            result = []
        }else {
            return  target
        }
        for (let i in target){
            let value = target[i]
            let valueType = checkType(value)
            if(valueType  === "Object" || valueType === "Array"){
                result[i] = deepClone(value)
            }else{
                result[i] = value
            }
        }
        return result
    }

    let arr1 = [1, 2, {age: 18},["codeing", "sleeping"]]
    let arr2 = deepClone(arr1)
    arr2[2].age = 34
    arr2[3][1] = "严钢我来自地球"
    console.log(arr1, arr2)
一起设计吧
下一篇: ES5类的继承
BACK