手写代码系列

深拷贝

   
        function deepCopy(obj) {
            let newObj = obj.constructor === 'Array' ? [] : {}; // 赋初值
            if (typeof obj !== 'object') return;

            for (let key in obj) {
                newObj[key] = typeof obj[key] === 'object' ? deepCopy(obj[key]) : obj[key];
            }
            return newObj;

        }

        let obj = {
            name: 'nanlan',
            age: '100',
            hobby: ["photography", "code"],
            skill: {
                specialty: "vue,angular,react,node,flutter",
                amateur: "take photo,reading"
            }
        }

        let newObj = deepCopy(obj);
        newObj.hobby[0] = 'skating';
        console.log(newObj);
        console.log(obj);

html转码

    htmlDecode(string){
             if(string.length ==0) return;
             return str.replace(/&/g, '&')
                             .replace(/'/g,''')
                              .replace(/"/g,'"')
                             .replace(/>/g,'>')
                            .replace('/</g','&lt;')
                        
       }

猜你喜欢

转载自blog.csdn.net/weixin_33971205/article/details/90915219