How can I directly access the [[PrimitiveValue]] value in JavaScript

Is there a way to directly access [[PrimitiveValue]] JavaScript object types, for example. String or number?

+6
source share
2 answers

Is there a way to directly access [[PrimitiveValue]] JavaScript object types, for example. String or Number ?

No no.

A primitive value is obtained by JS when an object is used in a context that requires a primitive, and it does this by calling the valueOf method of the object. Contexts that require primitive, as indicated in the comments, include +numberObj , '' + stringObj and "casting" via Number(numberObj) . Note that constructs such as Object(false) && true do not translate the logical object into a primitive, not if (Object(false)) (which is therefore successful).

A note can also be overridden by valueOf , in which case there are no cases where a basic primitive value can or will be obtained.

+4
source

No in a sense, because in regards the ES6 standard, [[Primitive Value]] is an internal conversion algorithm, not a result, taking an input value to be processed and an optional argument specifying the preferred type of the returned value. The algorithm is typically used in automatic type conversion and does not always return the same data type for a given input.

[[Primitive value]] algorithms can be divided into standard and exotic. The standard approach is that by default the preferred type of result is "number" if it is not specified. Exotic algorithms, such as those implemented for Date Objects , provide their own default value for the conversion result.

Of course, converting a String object to a string data type (for example, "" + strObj) or a Number object to a numeric data type (for example + numObj) is trivial if and when necessary. In most cases, this can be done automatically for you.

+4
source

All Articles