JavaScript equivalent of jQuery's extend method

前端 未结 8 2063
小鲜肉
小鲜肉 2020-11-28 02:57

Background

I have a function that takes a config object as an argument. Within the function, I also have default object. Each of those

8条回答
  •  甜味超标
    2020-11-28 03:33

    It helps me a lot when I develop with pure javascript.

    function extends(defaults, selfConfig){
         selfConfig = JSON.parse(JSON.stringify(defaults));
         for (var item in config) {
             if (config.hasOwnProperty(item)) {
                 selfConfig[item] = config[item];
             }
         }
         return selfConfig;
    }
    

提交回复
热议问题