user213154
user213154

Reputation:

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

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

Can its generic nature be retained? Or do we have to use the name of the constructor in place of arguments.callee?

Upvotes: 12

Views: 2390

Answers (1)

user578895
user578895

Reputation:

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.

Upvotes: 5

Related Questions