Aayla Secura
Aayla Secura

Reputation: 475

Are built-in static methods bound?

According to the specification, are built-in Javascript static methods, like Array.from, Object.assign, console.log, etc meant to be bound or do they need to be invoked via the constructor/object?

Consider the following:

const assign = Object.assign;
const o1 = assign({}, {x: 1}); // OK in Chrome & Firefox
const o2 = assign.call(Object, {}, {x: 1}); // saefest, but is it necessary?

Assigning one of these static functions to a variable and invoking without the original context seems fine in the modern browsers I tested, but I often see the use of the more verbose approach of setting this to the original context via Function.prototype.call or Function.prototype.apply.

Is this necessary in practice? I can't find anything on the specification that says whether the above mentioned method and similar ones are "context-free" so to speak, i.e. can be safely called from an aliased variable without setting thisArg.

Upvotes: 1

Views: 32

Answers (0)

Related Questions