I stumbled upon such usage in Array.prototype.includes() polyfill code:
var O = Object(this);
What are the semantics of this function? Links to documentation would be welcome too.
I stumbled upon such usage in Array.prototype.includes() polyfill code:
var O = Object(this);
What are the semantics of this function? Links to documentation would be welcome too.
The Object constructor, when called as a function, attempts to convert the argument to an object, as follows:
Object.prototype. Like if you used ({}).Examples:
var obj = [1,2,3], symb = Symbol();
// Object argument
Object(obj); // obj;
// Object-coercible argument
Object(true); // Boolean { [[BooleanData]]: true }
Object(1234); // Number { [[NumberData]]: 1234 }
Object("ab"); // String { [[StringData]]: "ab", 0: "a", 1: "b", length: 2 }
Object(symb); // Symbol { [[SymbolData]]: symb }
// Non-object-coercible argument
Object(null); // Object { }
Object(void 0); // Object { }
Object(); // Object { }
The polyfill uses Object(this) because the spec says Array.prototype.includes should use ToObject, but that's an internal thing which is not exposed to JS code.
Owould bewindow.Owould be the array on whichincludeswas called.