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

检测函数是否通过new运算符被调用的三种方法 #20

Open
OPY-bbt opened this issue Nov 23, 2019 · 0 comments
Open

检测函数是否通过new运算符被调用的三种方法 #20

OPY-bbt opened this issue Nov 23, 2019 · 0 comments

Comments

@OPY-bbt
Copy link
Owner

OPY-bbt commented Nov 23, 2019

  • 利用严格模式下,函数中this指向undefined,直接调用会报错
function Parent() {
  "use strict";
  this.name = "rss";
}
  • instanceof 判断this
function Parent() {
  if (!(this instanceof Parent)) { throw new Error();}
}
  • new.target(使用new方法调用时,指向构造函数,普通函数调用指向 undefined)
function Parent() {
  if (new.target !== Parent) { throw new Error();}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant