36

Let's assume we have the following code:

const myVariable = { age: 7, name: "Hunter" };

I want to know the name of the variable, not just the value of it, and put it in a string or log it or:

const s = nameof(myVariable) + ': ' + JSON.stringify(myVariable);

And the result I want to see is sth like:

"myVariable: {"age":7,"name":"Hunter"}"

Looking forward to see your solution.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
dr. rAI
  • 1,773
  • 1
  • 12
  • 15

1 Answers1

38

This is my own answer in Q&A style which comes with a bit of a compromise. The trick is here to pass the parameter to the function nameof inside an object by enclosing it in curly braces, like this nameof({myVariable}) instead of nameof(myVariable).

Following this convention, the solution can look like this:

var HD = HD || {};
HD.nameof = function (obj) {
  return Object.keys(obj)[0];
}

And this is how I use it to get the name of the variable/object plus the object's content/the variable's value:

const s = HD.nameof({myVariable}) + ': ' + JSON.stringify(myVariable);   

Thus, s will contain the result as requested in the question above.

dr. rAI
  • 1,773
  • 1
  • 12
  • 15
  • Note that this only works with ES6 where `{x}` is a shortcut for `{x: x}`. But at the moment I don't see the point because you already know the name of the variable! You could just write `const s = "myVariable: " + JSON.stringify(myVariable)`. What is the point? It would make sense if JavaScript had a preprocessor and you were looking for something like the `#` preprocessor prefix in C++, but it hasn't... (And if it were for debug output, you could just do `JSON.stringify({myVariable})` in the first place and you'd get `{"myVariable": {"age": 7, "name": "hunter"}}`...) – CherryDT May 05 '16 at 17:59
  • isn't `Object.keys(myVariable)[0];` just as simple as your method and much more obvious what's doing? – Liam May 05 '16 at 18:00
  • 2
    @Liam: No, the trick lies in the curly braces and [ES6's property shorthand feature](http://es6-features.org/#PropertyShorthand). Basically, he's doing `HD.nameof({myVariable: myVariable})`, i.e. `Object.keys({myVariable: myVariable})[0]` which is of course `"myVariable"`. – CherryDT May 05 '16 at 18:02
  • 15
    The difference between `"myVariable"` and `{myVariable}` does not seem to be huge for the human eye, but some intellisense algorithm or parser can find this distinction useful. I believe that's why it was introducted to C# 6 ([nameof in C#](https://msdn.microsoft.com/en-us/library/dn986596.aspx)). I like it and use it a lot in my C# code and with this compromise it is almost what a nameof operator could be in ES. – dr. rAI May 05 '16 at 18:12
  • 11
    it's really too bad this doesn't work easily for `foo.myVariable` – Maslow Aug 10 '17 at 18:54
  • @Maslow, The shorthand wouldn't work for foo.myVariable? If I do { foo.myVariable } what's the resulting object? If it is { myVariable: |valueOfMyVariable| } then should work exactly as C# nameof operator since it doesn't return fully qualified names. – Cleivson Arruda Aug 14 '17 at 12:39
  • doing ({window.name}) => Uncaught SyntaxError: Unexpected token '.' – IulianT Jan 14 '22 at 14:50