Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

10. 实现一个new操作符 #10

Open
maxlxq opened this issue Feb 10, 2020 · 0 comments
Open

10. 实现一个new操作符 #10

maxlxq opened this issue Feb 10, 2020 · 0 comments

Comments

@maxlxq
Copy link
Owner

maxlxq commented Feb 10, 2020

步骤

  1. 创建一个空的简单的JavaScript对象,即{}
  2. 链接该对象到另一个对象
  3. 将步骤1新创建的对象作为this的上下文
  4. 如果该函数没有返回对象,则返回this
function news() {
  let Constructor = [].shift.call(arguments);
  const target = {};
  target.__proto__ = Constructor.prototype;
  const res = Constructor.call(target, ...arguments);
  if (typeof res === 'object' || typeof res === 'function') {
    return res;
  }
  return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant