javascript ES5 Object.assign 等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30498318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:17:21  来源:igfitidea点击:

ES5 Object.assign equivalent

javascriptobjectecmascript-5ecmascript-harmony

提问by Saba Hassan

I wanted to do something which is very straight-forward using Object.assign.

我想做一些使用Object.assign.

var firstObj = {name : "Saba H.", rollNo : 1};
var secondObj = {college : "WCE"};
var wholeObj = Object.assign(firstObj, secondObj);

console.log(wholeObj); // {name : "Saba H.", rollNo : 1, college : "WCE"}

As Object.assignis part of ECMAScript6 harmony proposal and not supported across many browsers, is it possible to do with ES5? If not then is there any micro library?

作为Object.assignECMAScript6 和谐提案的一部分,并且不受许多浏览器的支持,ES5 可以吗?如果没有,那么是否有任何微型图书馆?

回答by Sareskaph

In underscore.js you can use like,

在 underscore.js 你可以使用像,

_.extend(firstObj, secondObj);

In jQuery, you can use,

在 jQuery 中,你可以使用,

$.extend({},firstObj,secondObj);

In pure javascipt, you can merge nnumber of objects by using this function:

在纯 javascipt 中,您可以n使用此函数合并多个对象:

function mergeObjects() {
    var resObj = {};
    for(var i=0; i < arguments.length; i += 1) {
         var obj = arguments[i],
             keys = Object.keys(obj);
         for(var j=0; j < keys.length; j += 1) {
             resObj[keys[j]] = obj[keys[j]];
         }
    }
    return resObj;
}

回答by HynekS

I found a working polyfill for Object.assign on MDN(awesome, thanks!):

我在MDN上找到了一个适用于 Object.assign 的polyfill(太棒了,谢谢!):

if (typeof Object.assign != 'function') {
  // Must be writable: true, enumerable: false, configurable: true
  Object.defineProperty(Object, "assign", {
    value: function assign(target, varArgs) { // .length of function is 2
      'use strict';
      if (target == null) { // TypeError if undefined or null
        throw new TypeError('Cannot convert undefined or null to object');
      }

      var to = Object(target);

      for (var index = 1; index < arguments.length; index++) {
        var nextSource = arguments[index];

        if (nextSource != null) { // Skip over if undefined or null
          for (var nextKey in nextSource) {
            // Avoid bugs when hasOwnProperty is shadowed
            if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
              to[nextKey] = nextSource[nextKey];
            }
          }
        }
      }
      return to;
    },
    writable: true,
    configurable: true
  });
}

回答by Stan

The pure javascript way:

纯javascript方式:

function mergeObjects(){
    var res = {};
    for(var i = 0;i<arguments.length;i++){
        for(var x in arguments[i]){
            res[x] = arguments[i][x];
        };
    };
    return res;
};

Example:

例子:

var my_merged_object = mergeObjects(obj1,obj2,...);

回答by Александр phpner

var extend = function ( defaults, options ) {
    var extended = {};
    var prop;
    for (prop in defaults) {
        if (Object.prototype.hasOwnProperty.call(defaults, prop)) {
            extended[prop] = defaults[prop];
        }
    }
    for (prop in options) {
        if (Object.prototype.hasOwnProperty.call(options, prop)) {
            extended[prop] = options[prop];
        }
    }
    return extended;
};
var settings = extend(defaults, options);