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

JavaScript模拟实现new操作符 #10

Open
GGXXMM opened this issue Aug 4, 2019 · 0 comments
Open

JavaScript模拟实现new操作符 #10

GGXXMM opened this issue Aug 4, 2019 · 0 comments
Labels
⭐️ js js knowledge

Comments

@GGXXMM
Copy link
Owner

GGXXMM commented Aug 4, 2019

new操作符究竟做了些什么事情:

  • 它创建了一个全新对象
  • new创建的对象通过__proto__链接到函数的原型
  • this指向新创建的对象
  • 如果函数没有返回对象类型Object,那么new表达式的函数调用会自动返回这个新对象

模拟new操作符

function myNew() {
  // 1. 创建一个新对象
  let obj = {};
  // 2. 创建的对象通过__proto__链接到函数的原型(继承函数的属性及方法)
  let fn = [].shift.call(arguments);
  obj.__proto__ = fn.prototype;
  // 3. this指向当前新对象
  let result = fn.apply(obj, arguments);
  // 4. 返回对象(若函数没有返回对象类型,那么自动返回new新创建的对象)
  return result instanceof Object ? result : obj;
}
@GGXXMM GGXXMM added the ⭐️ js js knowledge label Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐️ js js knowledge
Projects
None yet
Development

No branches or pull requests

1 participant