[英]How can I use Object.assign() to create this state object how I want it
[英]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.