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

13 javascript constructor arguments strict-mode

有一个老技巧(我在SO上学到的)将一个构造函数作为函数调用,即忘记new关键字.以下,或类似的东西,它位于顶部的每个构造函数中.

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

需要时有哪些替代方案"use strict";

它的通用性质可以保留吗?或者我们必须使用构造函数的名称来代替arguments.callee

Mar*_*ahn 5

arguments.callee不赞成使用本身的命名函数表达式.虽然我不一定同意这一举动,但事情已经取得了进展.因此,arguments.callee在严格模式下,用函数名替换是唯一的方法,也是非严格模式下的首选方式.