There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

hydroptic OP , (edited )

https://sopuli.xyz/pictrs/image/2d5316ef-c873-4f61-a6e9-021cb7fe1209.webp

Everything that’s an Object is going to either inherit Object.prototype.toString() (mdn) or provide its own implementation. Like I said in another comment, even functions have a toString() because they’re also objects.

A String is an Object, so it’s going to have a toString() method. It doesn’t inherit Object’s implementation, but provides one that’s sort of a no-op / identity function but not quite.

So, the thing is that when you say const someString = “test string”, you’re not actually creating a new String object instance and assigning it to someString, you’re creating a string (lowercase s!) primitive and assigning it to someString:

https://sopuli.xyz/pictrs/image/6687aa17-d750-41c9-ab7f-e8930375a9e6.webp

Compare this with creating a new String(“bla”):

https://sopuli.xyz/pictrs/image/ca5e8b71-ab4d-4788-aee7-f069041963ad.webp

In Javascript, primitives don’t actually have any properties or methods, so when you call someString.toString() (or call any other method or access any property on someString), what happens is that someString is coerced into a String instance, and then toString() is called on that. Essentially it’s like going new String(someString).toString().

Now, what String.prototype.toString() (mdn) does is it returns the underlying string primitive and not the String instance itself:

https://sopuli.xyz/pictrs/image/e0a06afa-2f26-492f-8f62-47654d2a9e83.webp

Why? Fuckin beats me, I honestly can’t remember what the point of returning the primitive instead of the String instance is because I haven’t been elbow-deep in Javascript in years, but regardless this is what String’s toString() does. Probably has something to do with coercion logic.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines