Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

模拟Object.assing #7

Open
luichooy opened this issue Aug 15, 2019 · 0 comments
Open

模拟Object.assing #7

luichooy opened this issue Aug 15, 2019 · 0 comments

Comments

@luichooy
Copy link
Owner

luichooy commented Aug 15, 2019

yygmind/blog#26 学习的,感谢大佬分享

// 模拟Object.assign方法
if (typeof Object.assign2 !== 'function') {
  Object.defineProperties(Object, 'assign2', {
    value: function(target) {
      if (target == null) {
        throw new TypeError('Cannot convert undefined or null to object');
      }
      let obj = Object(target);

      for (let i = 1, len = arguments.length; i < len; i++) {
        const source = arguments[i];

        if (source != null) {
          for (let key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              obj[key] = source[key];
            }
          }
        }
      }
      return obj;
    },
    configurable: true,
    writable: true
  });
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant