javascript - Typescript allows to create a class field name by starting with number - Stack Overflow

Why typescript allows class field name in number1: number = 1;2: number = 2;As per this discussion Why

Why typescript allows class field name in number

 1: number = 1;
 2: number = 2;

As per this discussion Why can't variable names start with numbers? We can't create a class field name starting with number(even JavaScript also does not allow, if we define a variable name is starting with number). But in typescript we can create a class field name starting with number(whatever). Why? and it's a bug in typescript?.

Reference

Why typescript allows class field name in number

 1: number = 1;
 2: number = 2;

As per this discussion Why can't variable names start with numbers? We can't create a class field name starting with number(even JavaScript also does not allow, if we define a variable name is starting with number). But in typescript we can create a class field name starting with number(whatever). Why? and it's a bug in typescript?.

Reference

Share Improve this question edited Aug 3, 2017 at 11:32 Ramesh Rajendran asked Aug 3, 2017 at 10:32 Ramesh RajendranRamesh Rajendran 38.7k49 gold badges159 silver badges240 bronze badges 6
  • 1 it doesn't, I'm not sure how you get that working? in the playground at least it doesn't typescriptlang/play/… – toskv Commented Aug 3, 2017 at 10:34
  • It does not accept inside the method. Am asking about when you declare the variable globally. – Ramesh Rajendran Commented Aug 3, 2017 at 10:37
  • Even then it does not work. Provide a plete verifiable example. – Murat Karagöz Commented Aug 3, 2017 at 10:38
  • @toskv and Murat I have attached the reference. Could you please explain for the down vote. – Ramesh Rajendran Commented Aug 3, 2017 at 10:40
  • 1 Those are not variables but class fields. – zerkms Commented Aug 3, 2017 at 10:41
 |  Show 1 more ment

1 Answer 1

Reset to default 7

Those definitions of 1 and 2 aren't variable definitions, you're defining class members, which is totally fine to name them by a number.

But remember you can't access them by this.1 you have to use this[1].

In Javascript object properties can be named by a number (which is for example what the implementation of Array is doing), because (as @zerkms statet in the ments) they're implicitly converted into strings.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745183656a4615535.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信