if (typeof Object.assignWhenSame != 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assignWhenSame", {
value: function assignWhenSame(target, varArgs) { // .length of function is 2
'use strict';
// TypeError if undefined or null
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
var to = new Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
// Skip over if undefined or null
if (nextSource != null) {
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey) && Object.prototype.hasOwnProperty.call(target, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
// test
console.log(Object.assignWhenSame({
a: 1
},
{
a: 2,
b: 3
}));
console