JavaScript equivalent of jQuery's extend method

前端 未结 8 2040
小鲜肉
小鲜肉 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条回答
  •  -上瘾入骨i
    2020-11-28 03:08

    You can loop through Object's properties using for statement.

    var settings = extend(default, config);
    
    function extend(a, b){
        var c = {};
        for(var p in a)
            c[p] = (b[p] == null) ? a[p] : b[p];
        return c;
    }
    

提交回复
热议问题