Assign
function assign(target, sources) {
<!--
这里将 target包装一下。如果target不是一个对象,而是一个常量,比如数字,字符串。这里包装了一下避免target是不是对象。
-->
var to = Object(target);
var hasOwnProperty = Object.prototype.hasOwnProperty;
for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
var nextSource = arguments[nextIndex];
if (nextSource == null) {
continue;
}
<!--
这里也是包裹了其他的参数,以防止参数不是对象,在遍历的时候出错。
-->
var from = Object(nextSource);
<!--
这里遍历一下,将属性添加到目标对象上。
-->
for (var key in from) {
if (hasOwnProperty.call(from, key)) {
to[key] = from[key];
}
}
}
<!--
返回目标对象
-->
return to;
}
总结一下
该方法就是将该方法第二个参数极其之后的参数的属性添加到目标对象上。