4

I'm working on a project where I need to deal with javacsript frameworks for work. We have a parser that reads through them, but errors on lines with .. such as

1..toPrecision()    

or

24..map(function(t){return 7..map(function(a){return e[a][t]})

It doesn't seem to understand the "..", and I don't either. Why is this valid javascript? How does mapping on a single number work? Eventually someone will fix the parser, but I'm looking for a temporary fix as to how I can edit the minified .js file to work. Is there another way to write something like 24..map()?

locks
  • 6,537
  • 32
  • 39
suhmedoh
  • 103
  • 1
  • 4

2 Answers2

10

It's kind of a funny situation. Numbers can have a value after the decimal point, right?

console.log(1.2345); // for example

Well, it's also possible to write a number with a decimal point without any numbers following it.

console.log(5.);

So the first dot is the decimal point. The second is the property accessor.

console.log(5.                  .toString());
//           ^ decimal point    ^ property accessor

The specification defines decimal literals as:

DecimalIntegerLiteral . DecimalDigits opt ExponentPart opt

where opt means optional.

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
4

The first . is the decimal separator character. 1. is a number.

The second . is the object property accessor. someNumber.toPrecision is a function.

Another way to write it would be to write the number with more significant figures:

1.0.toPrecision()
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • It's a short and _weird_ way of saying `1.0`. Is there any reason for it that you know of? – George Apr 03 '17 at 14:35
  • @George It's one byte shorter (we're talking _minified_). And, possibly, just because it _is_ weird (obfuscation). – TripeHound Apr 04 '17 at 08:05
  • @TripeHound I agree with the obfuscation but surely you could make it 2 bytes shorter by just writing `1` – George Apr 04 '17 at 08:16
  • 1
    @TripeHound — JavaScript doesn't have integers. It only has *Numbers*. `1.toPrecision()` throws a syntax error because the `.` is the decimal point, not the property accessor. You can't put a letter on the right hand side of a decimal point. – Quentin Apr 04 '17 at 08:25