子类继承父类原型解决方法:深拷贝、组合继承

1.深拷贝

//深拷贝
        function deepCopy(obj) {
            let newObj = Array.isArray(obj) ? [] : {}
            for (const key in obj) {
                if (Object.hasOwnProperty.call(obj, key)) {
                    if (typeof obj[key] == 'object') {
                        newObj[key] = deepCopy(obj[key])
                    } else {
                        newObj[key] = obj[key];
                    }
                }
            }
            return newObj
        }

  Son.
prototype = Dad.prototype

 

2.组合继承

        let Link = function(){}
        Link.prototype = Dad.prototype
        Son.prototype = new Link()
        Son.prototype.constructor = Son

 

posted @ 2021-05-30 11:09  珂珂keo  阅读(111)  评论(0编辑  收藏  举报