我想通过 Javascript Object.assign() 将一些对象分配给目标对象。它在 chrome 中运行良好。尽管在 Safari 中出现错误。
TypeError: 'undefined' is not a function (evaluating 'Object.assign(wkObj,tObj,list)')
对象
var list = {'width':'100px'};
var tz = "translateZ(-18px)";
var tObj = {'transform': tz};
var wkObj = {'-webkit-transform': tz};
最佳答案
safari 不支持它,但您始终可以创建一个 polyfill:
if (!Object.assign) {
Object.defineProperty(Object, 'assign', {
enumerable: false,
configurable: true,
writable: true,
value: function(target, firstSource) {
'use strict';
if (target === undefined || target === null) {
throw new TypeError('Cannot convert first argument to object');
}
var to = Object(target);
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];
if (nextSource === undefined || nextSource === null) {
continue;
}
var keysArray = Object.keys(Object(nextSource));
for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
var nextKey = keysArray[nextIndex];
var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
if (desc !== undefined && desc.enumerable) {
to[nextKey] = nextSource[nextKey];
}
}
}
return to;
}
});
}
或者开始使用类似 Babel 的转译器
Reference for polyfill
关于javascript - Safari 类型错误 : 'undefined' is not a function (evaluating 'Object.assign(...)' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39326907/