JavaScript 等效于 jQuery 的扩展方法 [英] JavaScript equivalent of jQuery's extend method

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

问题描述

我有一个将 config 对象作为参数的函数.在函数中,我还有 default 对象.这些对象中的每一个都包含本质上用作函数内其余代码的设置的属性.为了避免在 config 对象中指定所有设置,我使用 jQuery 的 extend 方法来填充一个新对象,settings使用 default 对象中的任何默认值,如果它们没有在 config 对象中指定:

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 的情况下重现此功能.是否有同样优雅(或接近)的方法来使用普通的 ol' 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 的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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