jquery - Creating a config for a function in JavaScript - Stack Overflow

I want my JavaScript class to be configurable from the outside. So, I created a private config-property

I want my JavaScript class to be configurable from the outside. So, I created a private config-property and a loadConfig-method. My problem is, that when I load a config, it overwrites the config-property pletely and not just the properties I defined.

(function() {
var config = {
    param: value,
    group: {
        groupParam: value
    }
};
function MyClass() {
    this.loadConfig = function(loadConfig) {
        if (typepof loadConfig == "undefined" || typeof loadConfig == "null")
            return;
        else
            config = loadConfig;
    };  
}
window.MyClass = new MyClass();
})();

When I load a custom config

<script type="text/javascript">
    var config = {
        group: {
            groupParam: "newValue"
        }
    };

    MyClass.loadConfig(config);
</script>

I want config.param to still be "value", while config.group.groupParam is "newValue".

Currently, the object is overwritten and after loadConfig(), config.param doesn't exist anymore.

I want my JavaScript class to be configurable from the outside. So, I created a private config-property and a loadConfig-method. My problem is, that when I load a config, it overwrites the config-property pletely and not just the properties I defined.

(function() {
var config = {
    param: value,
    group: {
        groupParam: value
    }
};
function MyClass() {
    this.loadConfig = function(loadConfig) {
        if (typepof loadConfig == "undefined" || typeof loadConfig == "null")
            return;
        else
            config = loadConfig;
    };  
}
window.MyClass = new MyClass();
})();

When I load a custom config

<script type="text/javascript">
    var config = {
        group: {
            groupParam: "newValue"
        }
    };

    MyClass.loadConfig(config);
</script>

I want config.param to still be "value", while config.group.groupParam is "newValue".

Currently, the object is overwritten and after loadConfig(), config.param doesn't exist anymore.

Share edited Jun 10, 2011 at 7:42 kapa 78.7k21 gold badges165 silver badges178 bronze badges asked May 27, 2011 at 7:12 F.PF.P 17.9k34 gold badges126 silver badges194 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

If you don't want to reinvent the wheel, jQuery has a great function called $.extend() which seems to be what you need.

You can check it out in the jQuery 1.6.1 source for example ( Ctrl + F and "extend" => been a long time since I've been waiting to use these keyboard things once :) ).

It's overwritten because you're telling it to overwrite with this line in your loadConfig method:

config = loadConfig;

In order to preserve any values that loadConfig does not explicitly change, you'll have to loop through all of the properties in loadConfig and assign them individually:

this.loadConfig = function (loadConfig) {
    if (typeof loadConfig == "undefined" || typeof loadConfig == "null") {
        return;
    } else {
        var x;
        for (x in loadConfig) {
            if (Object.prototype.hasOwnProperty.call(loadConfig, x)) {
                config[x] = loadConfig[x];
            }
        }
    }
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744883596a4598986.html

相关推荐

  • jquery - Creating a config for a function in JavaScript - Stack Overflow

    I want my JavaScript class to be configurable from the outside. So, I created a private config-property

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信