繁体   English   中英

我如何在 Internet Explorer 11 中使用 object.assign

[英]How do i use object.assign in internet explorer 11

我有使用 Object.assign 的 javascript 代码,这在除 Internet Explorer 11 之外的所有浏览器中都可以正常工作。

在此示例中 Object.assign(options, { 失败并出现 Object 不支持属性或方法“分配”

如何重写它以兼容包括 IE11 在内的所有浏览器?

var options = {
    authorization: braintreePayment.token,
    container: '#braintree-container',
    vaultManager: true,
    threeDSecure: threeDSecureFlag,
};

if (braintreePayment.isPaypal) {
    Object.assign(options, {
        paypal: {
            flow: 'vault',
        },
    });
}

if (braintreePayment.isPaypalCredit) {
    Object.assign(options, {
        paypalCredit: {
            flow: 'checkout',
            amount: braintreePayment.total,
            currency: braintreePayment.currency,
        },
    });
}

if (braintreePayment.isApplePay) {
    Object.assign(options, {
        applePay: {
            displayName: braintreePayment.companyName,
            paymentRequest: {
                total: {
                    label: braintreePayment.companyName,
                    amount: braintreePayment.total,
                },
                requiredBillingContactFields: ['postalAddress'],
            }
        }
    });
}

您可以使用来自 MDN 的这个polyfill https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

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 || target === undefined) {
        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 && nextSource !== undefined) { 
          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
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM