jQuery $ .extend不会覆盖我的设置值

时间:2017-06-21 13:09:01

标签: jquery frameworks

我正在尝试编写自己的框架,但我在覆盖项目的默认设置方面存在问题。起初我想用一个循环用设置替换defaultSettings然后我找到$ .extend,这应该更容易。但是我似乎无法用我的设置覆盖defaultSettings

这些是defaultSettings

var defaultSettings = {
    authenticFilter: 'off',
    randomizeHeaders: {state: 'off', topHeaderElement: 'h1', bottomHeaderElement: 'h2', topHeaderClass: ["hl1", "hl3"], bottomHeaderClass: ["hl4", "hl2"]}, 
    hoverFilter: 'off', 
}

这些是应该覆盖defaultSettings的设置

var settings = {
    authenticFilter: 'on', 
    randomizeHeaders: {state: 'on', topHeaderElement: 'h1', bottomHeaderElement: 'h2', topHeaderClass: ["hl1", "hl3"], bottomHeaderClass: ["hl4", "hl2"]}, 
    hoverFilter: 'on'
};

我们都试过了

var options = $.extend(true, defaultSettings , settings);

 var options = $.extend({}, defaultSettings , settings); 

但似乎都不起作用,我的代码默认为defaultSettings。是延伸到这里的方式,如果是这样,为什么这不起作用?



    var defaultSettings = {
        authenticFilter: 'off',
        randomizeHeaders: {state: 'off', topHeaderElement: 'h1', bottomHeaderElement: 'h2', topHeaderClass: ["hl1", "hl3"], bottomHeaderClass: ["hl4", "hl2"]}, 
        hoverFilter: 'off', 
    }

var settings = {
    authenticFilter: 'on', 
    randomizeHeaders: {state: 'on', topHeaderElement: 'h1', bottomHeaderElement: 'h2', topHeaderClass: ["hl1", "hl3"], bottomHeaderClass: ["hl4", "hl2"]}, 
    hoverFilter: 'on'
};

var options = $.extend(true, defaultSettings, settings);
console.log(options);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

0 个答案:

没有答案