简体   繁体   English

这个instanceof arguments.callee的严格模式替代

[英]Strict mode alternatives to this instanceof arguments.callee

There's an old trick ( that I learned on SO ) to catch calling a constructor as a function, ie forgetting the new keyword. 有一个老技巧( 我在SO上学到 )将一个构造函数作为函数调用,即忘记new关键字。 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"; 当你需要"use strict";时,有哪些选择"use strict"; ?

Can its generic nature be retained? 它的通用性质可以保留吗? Or do we have to use the name of the constructor in place of arguments.callee ? 或者我们是否必须使用构造函数的名称来代替arguments.callee

arguments.callee itself is deprecated in favor of named function expressions. 不推荐使用arguments.callee本身来支持命名函数表达式。 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. 因此,使用函数名替换arguments.callee是严格模式下的唯一方法,也是非严格模式下的首选方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM