Why can’t I call methods directly in a Javascript number?

Asked

Viewed 153 times

22

A simple question, but it’s making me curious.

I know that in javascript everything is warpado in some object constructor, so I can call string methods even if the string is not an object in theory. So when do I:

"Hello World".split(' ');

I’m calling the method split() inside the object String javascript.

My question is: why are numbers not equally warpados? I say:

2.toString(); // não funciona

var dois = 2;
dois.toString; // '2'

Why this behavior?

@Edit

I noticed that some methods can be called directly in numbers, such as toFixed() which makes me even more curious. The toString() does not work because it is inherited from Object?

2 answers

25


Yes! The problem is just the syntax of your test.

Notice that with just a small adjustment it already works:

console.log( (2).toString() );

What happens is that the interpreter will not understand the point followed by alphanumeric characters, and it is already used to separate decimals. By adding something that mischaracterizes this use (in the example, the parentheses), the interpreter has no more problems with the point.

In the case of .split() will no longer work, but it is by the simple fact that the method does not exist for numbers:

console.log( (432).split('') ); // Dá erro

But... look at this - converting to string, the split starts to work:

console.log( (432).toString().split('') );

The parentheses were an example, see other situations without ambiguity:

console.log( 432 .toString().split('') );

console.log( 789.0.toString().split('') ); // o segundo ponto não dá confusao

(the space example was suggested by colleague @jsbueno in the comments)

  • 4

    So in case it didn’t work because he considered the point a decimal separator and waited another number after him? Very interesting, thank you for the explanation.

13

That’s how it works:

console.log((2).toString());

It is a matter of syntax ambiguity of the language. JS allows a number to be expressed as decimal without filling the decimal part, that is, allows you to put only the integer number and a point and nothing else, which would create ambiguity for the call of the method starting with point.

console.log(2.);
console.log((2.).toString());
console.log(2.5.toString());

How would it be right? Ignore the decimal point? It seems very wrong. Use twice the dot? Strange and would prevent a syntax using this pattern (crease), then it was preferred to require that literal numbers could only be called by methods within parentheses or with space between the number and the point of the method (see credit in the comment below).

console.log(2. .toString());
console.log(2 .toString());
//console.log(2.toString());
//console.log(2..toString());

A mistake was made in the language to correct another error, only Javascript had chosen to require that the decimal part always be written explicitly, even if it is only a 0. It does not require, but you can do so and avoid major problems, so my recommendation is always do so for your case, it seems to me the most intuitive and matches with other languages:

console.log(2.0.toString());

And according to the issue in the question, the toFixed() also does not allow, the problem is ambiguous syntax and nothing with respect to the method itself or object it belongs to. Note that if you use a full decimal (integer plus decimal part, you can use without problems):

console.log(2.5.toString());
//console.log(2.toFixed());

I put in the Github for future reference.

  • 3

    In addition to parentheses, a space between the numerical token and the point to separate methods and properties also works.

  • @jsbueno this I did not know, thank you.

  • 1

    @Maniero the toFixed worked for me when I edited that because I used a decimal number to do the test, after all using the toFixed an entire number makes no sense. When I edited that I did the test like this: 2.73.toFixed() and it worked, so I edited that.

  • @Máttheusspoo just edited that works even on toString(), when you have the decimal part there is no ambiguity in the syntax, again the problem is syntax and not method, so I did in the toString() even to show that in this case works in all methods. Your question does not mention how it did, so it is difficult to show the real problem.

Browser other questions tagged

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