73.实现object.assign
Opened this issue · 0 comments
webVueBlog commented
Object.getOwnPropertyDescriptor(Object, 'assign');
// {
// value: f,
// writable: true, // 可写
// enumerable: false, // 不可枚举,注意这里是false
// configurable: true // 可配置
// }
Object.propertyIsEnumerable('assign'); // false
// es5
if (typeof Object.assign2 !== 'function') {
Object.defineProperty(Object, 'assign2', {
value: function (target) {
'use strict';
if (target === null) {
throw new Error('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) {
for (var nextKey in nextSource) {
// 由于目标对象可能是由 Object.create(null) 构建
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
enumerable: false; // 不可枚举
writable: true,
configurable: true
})
}