JavaScript相当于jQuery的extend方法 [英] JavaScript equivalent of jQuery's extend method

查看:187
本文介绍了JavaScript相当于jQuery的extend方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它将 config 对象作为参数。在函数中,我还有默认对象。这些对象中的每一个都包含基本上用作函数内其余代码的设置的属性。为了防止必须在 config 对象中指定所有设置,我使用jQuery的 extend 方法来填写一个新对象,设置,如果<$ c c中没有指定默认对象中的任何默认值$ c> config object:

I have a function that takes a config object as an argument. Within the function, I also have default object. Each of those objects contains properties that essentially work as settings for the rest of the code within the function. In order to prevent having to specify all of the settings within the config object, I use jQuery's extend method to fill in a new object, settings with any default values from the default object if they weren't specified in the config object:

var config = {key1: value1};
var default = {key1: default1, key2: default2, key 3: default 3};

var settings = $.extend(default, config);

//resulting properties of settings:
settings = {key1: value1, key2: default2, key 3: default 3};



问题



这很有效,但是我想在不需要jQuery的情况下重现这个功能。是否有一个同样优雅(或接近)的方法来做普通的'javascript?

Problem

This works great, but I'd like to reproduce this functionality without the need for jQuery. Is there an equally elegant (or close to) means to do this with plain ol' javascript?

此问题与如何动态合并两个JavaScript对象的属性?问题。虽然这个问题只是想创建一个包含来自两个独立对象的所有键和值的对象 - 我特别想要解决如何在两个对象共享一些但不是所有键以及哪个对象将优先的情况下执行此操作(如果存在重复键,则生成的对象的默认值。更具体地说,我想解决使用jQuery的方法来实现这一点,并找到一种没有jQuery的替代方法。虽然这两个问题的答案很多重叠,但这并不意味着问题本身是相同的。

This question is not a duplicate of the "How can I merge properties of two JavaScript objects dynamically?" question. Whereas that question simply wants to create an object that contains all of the keys and values from two separate objects - I specifically want to address how to do this in the event that both objects share some but not all keys and which object will get precedence (the default) for the resulting object in the event that there are duplicate keys. And even more specifically, I wanted to address the use of jQuery's method to achieve this and find an alternative way to do so without jQuery. While many of the answers to both questions overlap, that does not mean that the questions themselves are the same.

推荐答案

要获得结果你的代码,你会这样做:

To get the result in your code, you would do:

function extend(a, b){
    for(var key in b)
        if(b.hasOwnProperty(key))
            a[key] = b[key];
    return a;
}

请记住,你在那里使用扩展的方式将修改默认对象。如果您不想这样,请使用

Keep in mind that the way you used extend there will modify the default object. If you don't want that, use

$.extend({}, default, config)

模仿jQuery功能的更强大的解决方案如下:

A more robust solution that mimics jQuery's functionality would be as follows:

function extend(){
    for(var i=1; i<arguments.length; i++)
        for(var key in arguments[i])
            if(arguments[i].hasOwnProperty(key))
                arguments[0][key] = arguments[i][key];
    return arguments[0];
}

这篇关于JavaScript相当于jQuery的extend方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆