严格模式替代这个的instanceof arguments.callee的 [英] Strict mode alternatives to this instanceof arguments.callee

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

问题描述

有一个老把戏(,我学会了对SO )搭上调用构造函数作为一个功能,即不忘关键字。以下,或类似的东西,它会在每个构造顶部。

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

有什么选择,当您需要使用严格?

可在其泛用性予以保留?还是我们已经制定是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 本身是有利于命名的功能前pressions的pcated德$ P $ 。虽然我不一定有这个举动同意,这是事情进展如何。因此,替换是arguments.callee 的函数名是严格模式的唯一方法,也是在非严格模式下的preferred方式。

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天全站免登陆