Are all Javascript values, except primitives, objects?

Asked

Viewed 282 times

11

The documentation at w3school says All Javascript values, except primitives, are objects, which are they:

string
number
boolean
null
undefined

So

let nome = 'João';

It’s primitive string type, and why I can access methods ?

console.log(typeof nome); //retorna o tipo string
  • In the Mozilla is written > Virtually all Javascript objects descend from Object; all methods and properties inherited from Object.prototype, although they can be overwritten (except for a Null Prototype Object, i.e. Object.create(null)). For example, other prototypes builders override the constructor property and provide their own toString() methods.

  • So my variable is of the primitive string type, which string is an object so I can access the obj string methods ?

1 answer

9


W3schools has never been reliable and the specification uses universally wrong terms. One of the mistakes is to think that there are things that are objects and others that are not. Everything is an object. There are objects by reference and by value and there may be a differentiation there. Perhaps what they say are objects is what comes from Object.

The concept of primitive is not very well conceived, some understand that they are objects that the processor understands, so only the basic numeric types, which can include a single simple character (JS does not have this type of data) would be primitive. The idea is that the data is rudimentary, so very simple.

Another definition is that language gives special treatment to the type (would be builtin) and so it would be a primitive, in the sense of original.

In the specification of Ecmascript they invented a proper meaning for the term and chose by hand what they will call primitive and what they will call "object". Only those who wrote this know because each type is one thing or another, but it seems, at first glance, that it follows the line of having special treatment of language and therefore Object (which also has a special treatment, is builtin, but for some reason they thought it should not be considered primitive) and the types created by the programmer are not primitive. And for some reason a array is regarded as a Object and receives no discrimination, and he gets special treatment.

A Primitive value is a datum that is represented directly at the lowest level of the language implementation

This is in the specification and does not mean anything alone, I would need more explanation of what is "the lowest level of language implementation", which seems to be the first definition I gave, but there String is wrong, probably Null and Undefined also, so trying to find a logic in the definition will not get anywhere.

Or maybe they wanted to consider what kind of worthwhile guys were primitive, and String was considered as having semantics by value even if it is a type by reference. Perhaps this explains the exception you are thinking has.

They made the same mistake that Java made, it would be much better if everything was one Object, although some types were by value.

In fact, if you think something’s wrong, you should justify why you think it. If the language decided to be so for some reason, for its context is right and does not fit dispute. It may fit explanation, but remember that this language began with a name, then changed (Ecmascript) to be standardized and only then was created a specification on top of something already implemented in a trampled way in record time, so a lot of things came out crooked, so we have to accept that it is like this, only this.

But reading the specification a little more start talking about other definitions of what seems the same thing, it is not easy to understand. I’ve seen JS implementers say that there’s a lot of ambiguity in the specification and it’s hard to make a decision, and so there are implementations that do different things, each one understood a way.

So the question doesn’t even make much sense. A guy string is an object, which by one definition is a primitive, in another it is not, therefore it is irrelevant that difference, and objects can have methods.

It’s not just String that can access methods, other primitives can also, just are not the same methods, having methods is not something exclusive of a data type, only this can be taken as truth. Behold:

console.log(12..toFixed(2));
console.log(true.valueOf());

I put in the Github for future reference.

That is, it is all very confusing because it is poorly defined. If you want to see it starts at Section 4.2 of the current specification.

  • I thought it was dup de https://answall.com/questions/75676, but after this answer I don’t know anymore, it changed my reading of the question.

  • 1

    It’s similar but I think the focus is different, it’s more about existing methods in the "primitive" types, so I thought I should try to find a bigger explanation for the confusion of the PA, a lot of the answer is because I like to go beyond the "it’s like this and good" that is the penultimate paragraph. I think one complements the other, I’ll try to improve because I think it’s important. I’ll get better, because every time I reread I remember something else or I can make the text clearer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.