2

example:

var something = "some text";

test(something);

function test(param){
printParamName(param);
}

//console
"something"

I want to print in console: "something" the name of variable which I pass to that call to test function.

UPDATE

The variable is in a file and the function is in another file...

It's to complicate to explain the reason.... but I want to avoid to hardcode parameter of another function which is in test function for example.

file1:

var something = "some text";

test(something);

file2:

function test(param){
    var something = anotherFunctionCall('something'); //I want to put here nameof var something.
    var somethingElse = anotherFunctionCall('somethingElse');
}

file3:

another component "anotherFunctionCall"

I want to avoid that hardcoded name. I want to pass name throw that function.

Alex
  • 1,013
  • 1
  • 13
  • 27
  • :))) how to do that? how to print the name of object which I pass to that function call. – Alex Feb 28 '17 at 12:24
  • 1
    Unfortunately, this would require a `nameof` operator, which the TypeScript team is [reluctant](https://github.com/Microsoft/TypeScript/issues/1579) to implement, God knows why. The closest you can do is answered here: http://stackoverflow.com/questions/29191451/get-name-of-variable-in-typescript – John Weisz Feb 28 '17 at 12:25
  • I saw that but not helps me :D – Alex Feb 28 '17 at 12:26
  • 1
    No, you cannot do that, and never will be able to, in TS or any other JS variant. What would it return for `test(something+1)`? What is passed to a function as a parameter is just a value, no matter where it came from or how it was computed. If you could do this, what were you planning to use the feature for? By the way, it is not a "property" being passed, it's a "variable". –  Feb 28 '17 at 12:27
  • 2
    @torazaburo -- *"you cannot do that, and never will be able to"* -- Wrong, it has already been done in a fork. I'm not going to look it up now, but it is mentioned in the linked issue. Obviously, a theoretical `nameof` would only support a _subset_ of all expressions, and what you provided as an example would be a _clear_ compile error. – John Weisz Feb 28 '17 at 12:29
  • 2
    Possible duplicate of [Get name of variable in typescript](http://stackoverflow.com/questions/29191451/get-name-of-variable-in-typescript) – John Weisz Feb 28 '17 at 12:30
  • 1
    *"It's to complicate to explain the reason"* -- I wouldn't say so, what you are asking for is basically a `nameof` operator, which sees common use in implementing languages, such as C#: argument names for exceptions, dynamic object model mapping, etc. – John Weisz Feb 28 '17 at 12:41
  • @JohnWeisz No, I think this is a bit different. `nameof(param)` would return `"param"`, not `"something"`, which is what he wants. – Seamus Feb 28 '17 at 14:28
  • @Seamus -- Well, he can just do `test(nameof(something))` – John Weisz Feb 28 '17 at 14:34
  • @JohnWeisz If that's acceptable then he could do `test(something, "something")`. – Seamus Feb 28 '17 at 14:45
  • 1
    @Seamus -- I don't quite agree with that, because `"something"` creates a _magic-string_, which will (1) break on refactoring, (2) be left unchecked on compilation. – John Weisz Feb 28 '17 at 14:50

0 Answers0