Variable name as a string in Javascript

前端 未结 17 1548
难免孤独
难免孤独 2020-11-22 06:20

Is there a way to get a variable name as a string in Javascript? (like NSStringFromSelector in Cocoa)

I would like to do like this:

var myFirstName =         


        
17条回答
  •  太阳男子
    2020-11-22 06:46

    Typically, you would use a hash table for a situation where you want to map a name to some value, and be able to retrieve both.

    var obj = { myFirstName: 'John' };
    obj.foo = 'Another name';
    for(key in obj)
        console.log(key + ': ' + obj[key]);

提交回复
热议问题