此 instanceof arguments.callee 的严格模式替代方案 [英] Strict mode alternatives to this instanceof arguments.callee

查看:23
本文介绍了此 instanceof arguments.callee 的严格模式替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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");
}

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

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

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

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 替换为函数名是严格模式下的唯一方式,也是非严格模式下的首选方式.

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.

这篇关于此 instanceof arguments.callee 的严格模式替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆