Skip to content Skip to sidebar Skip to footer

Why Does "this" In String.prototype Return An Object?

This might be a duplicate, dumb or off-topic but the actual question is the question's title. I wanted to make something simple as this: String.prototype.log = function() { conso

Solution 1:

When you create a string you actually create an instance (object) of the string function.

Type String in the chrome console.

console.log(String);
Output : functionString()

If you create a string

var x = newString("aaa");

x is actually an instance of the String function

console.log(x instanceofString); // true

Why x is instance of Object?

In JavaScript every object has a __proto__ link.

x.__proto__ == String.prototype;

It means x's proto link points to the String prototype.

Now String.prototype has a __proto__ too.

String.prototype.__proto__ == Object.prototype;

instanceof operator first search for __proto__ link and follows it.

x's proto link points to String's prototype and String's prototype points to Object prototype.

Hence x is an instance of Object too.

Solution 2:

Not sure but found something that might answer the question:

String(Value)

When String is called with argument value, the following steps are taken:

If no arguments were passed to this function invocation, let s be "".

Else,

If NewTarget is undefined and Type(value) is Symbol, return SymbolDescriptiveString(value).

Let s be ToString(value).

ReturnIfAbrupt(s).

If NewTarget is undefined, return s.

Return StringCreate(s, GetPrototypeFromConstructor(NewTarget, "%StringPrototype%")).

The length property of the String function is 1.

Now StringCreate returns a String exotic object.

According to it,

A String object is an exotic object that encapsulates a String value and exposes virtual integer indexed data properties corresponding to the individual code unit elements of the String value.

Exotic String objects always have a data property named "length" whose value is the number of code unit elements in the encapsulated String value. Both the code unit data properties and the "length" property are non-writable and non-configurable.

Solution 3:

The best answer I can find, pertaining directly to this question, is answered in the ECMAScript specification on which JavaScript is based.

Here's the specification for String... http://www.ecma-international.org/ecma-262/5.1/#sec-8.4

An excerpt:

The String type is the set of all finite ordered sequences of zero or more 16-bit unsigned integer values (“elements”). The String type is generally used to represent textual data in a running ECMAScript program, in which case each element in the String is treated as a code unit value (see Clause 6). Each element is regarded as occupying a position within the sequence. These positions are indexed with nonnegative integers. The first element (if any) is at position 0, the next element (if any) at position 1, and so on. The length of a String is the number of elements (i.e., 16-bit values) within it. The empty String has length zero and therefore contains no elements.

Especially this part:

Each element is regarded as occupying a position within the sequence. These positions are indexed with nonnegative integers.

This explains why the actual truer storage of the String being revealed is array-like in nature. The characters are INDEXED sequentially as is mandated within the specification.

Solution 4:

The primitive string "test" is boxed into an object of type String before the log function is invoked.

Post a Comment for "Why Does "this" In String.prototype Return An Object?"