Strict mode alternatives to this instanceof arguments.callee

夙愿已清 提交于 2019-12-09 15:06:44

问题


There's an old trick (that I learned on SO) to catch calling a constructor as a function, i.e. forgetting the new keyword. The following, or something like, it goes in each constructor at the top.

if (!(this instanceof arguments.callee)) {
    throw Error("Constructor called as a function");
}

What are the alternatives when you need to "use strict"; ?

Can its generic nature be retained? Or do we have to use the name of the constructor in place of arguments.callee?


回答1:


arguments.callee itself is deprecated in favor of named function expressions. Although I don't necessarily agree with this move, it is how things have progressed. As such, replacing arguments.callee with the function name is the only way in strict mode, and is also the preferred way in non-strict mode.



来源:https://stackoverflow.com/questions/6480147/strict-mode-alternatives-to-this-instanceof-arguments-callee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!