Sagiv Ofek
Sagiv Ofek

Reputation: 25270

Number prototype definition

Can someone explain me how to overcome this thing?

String.prototype.one = 1;
"one".one; //returns 1

Number.prototype.one = 1;
1.one; //returns 'SyntaxError: Unexpected token ILLEGAL'

Upvotes: 2

Views: 69

Answers (1)

I Hate Lazy
I Hate Lazy

Reputation: 48771

This is because the interpreter sees a . after a number as a decimal, not a property accessor, so it sees it as this:

(1.)one  // SyntaxError

Give it another ., and it'll work.

1..one

It now sees it as this:

(1.).one

Other solutions:

1.0.one
1["one"]
(1).one

Upvotes: 9

Related Questions