【问题标题】:Strict mode alternatives to this instanceof arguments.callee此 instanceof arguments.callee 的严格模式替代方案
【发布时间】:2011-09-22 17:39:18
【问题描述】:

有一个老技巧 (that I learned on SO) 将构造函数作为函数调用,即忘记 new 关键字。下面的,或者类似的,它放在顶部的每个构造函数中。

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

当您需要"use strict"; 时,有哪些替代方案?

可以保留其通用性吗?还是我们必须使用构造函数的名称来代替arguments.callee

【问题讨论】:

    标签: javascript constructor arguments strict-mode


    【解决方案1】:

    arguments.callee 本身已被弃用,取而代之的是命名函数表达式。虽然我不一定同意这一举动,但事情就是这样发展的。因此,将arguments.callee替换为函数名是严格模式下的唯一方式,也是非严格模式下的首选方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多