Home > Enterprise >  Why is toString always running in strict mode?
Why is toString always running in strict mode?

Time:09-26

I know that according to ECMAScript specification, non-strict methods will have thisArg transformed to the global object, if null or undefined was passed to them. That's the reason this code logs the window object (in browsers) instead of null:

function x() {
    console.log(this);
}
x.call(null);

What I don't understand, is why doing the same thing with Object.prototype.toString.call(null), doesn't transform null to the global object, but keeps it as null and returns the string [object Null].

Is it because 'use strict' is used in default implementation of toString method? Is every built-in method run in strict-mode?

I couldn't find anything like that in the specification a https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.prototype.tostring.

CodePudding user response:

Is every built-in method run in strict-mode?

Yes. It says so in §10.3 Built-in Function Objects:

"Built-in functions that are ECMAScript function objects must be strict functions."

This not only implies non-sloppy handling of the this argument, but also that these functions have no .arguments, .callee and .caller properties.

  • Related