0

I'm wondering if i can get something like this:

function foo() {
    console.log(xxx);
}

var myFunc = new foo();

I want console.log to give me 'myFunc' string (or object, or anything I can reference to for that matter)

Is that at all possible in JavaScript?

Sorry for my poor language - not a native speaker and a self-taught developer :)

EDIT

Maybe I should explain the reason for why I "need" this. Let's say I have a plugin that creates an interactive object in DOM. I can receive events from other object (WebSocket) that can be viable to any created object, but I can't be sure which one. Of course, I can handle this by creating some helper objects and/or functions, but it would really help to make my code a bit clearer if I could reference that object based on some DOM element that exists on the page.

It would be great if I could add a data attribute to an element that contains said created interactive object, and then - by finding a class name that exists within it - reference a variable that was constructed based on that constructor function.

Yes, I realize that this is not the best way to handle this situation, but:

  1. This would reduce code complexity in my case
  2. I'm just wandering if it's possible :)
Koval
  • 145
  • 8
  • You don't have to use `new` keyword. `new` keyword can only be used for using class. This is correct `var myFunc = foo()` – Ilyas karim Mar 23 '17 at 21:13
  • 4
    @Ilyaskarim JavaScript doesn't have classes (at least traditionally). `new` can be used with any function. – melpomene Mar 23 '17 at 21:16
  • @FelixKling The question was how to print `myFunc`, which is the name of the variable, not the constructor function. – melpomene Mar 23 '17 at 21:19
  • @melpomene: Ah, I see now. – Felix Kling Mar 23 '17 at 21:20
  • He want something like this: `var myVariable = 12;`. `console.log(myVariable.getVariableNameAsString())`. – Ilyas karim Mar 23 '17 at 21:23
  • The only way to achieve this would be to iterate over the global object and compare the value of each global variable to the return value of the function. Of course that implies that the variable must be global. Also this logic would have to be invoked *after* the assignment. This is highly unusual, so the question is, why do you need this? If your code depends on the naming if a variable the you have a design problem. – Felix Kling Mar 23 '17 at 21:23
  • @koval Why did you need to get function's constructor's variable name? – Ilyas karim Mar 23 '17 at 21:25
  • I think you can find more here: http://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript – Ilyas karim Mar 23 '17 at 21:27
  • is it not possible for you to pass the name in when instantiating? Ie. `var myFunc = new foo('myFunc')` and just go from there? – Rooster Mar 23 '17 at 21:30
  • 1
    @Rooster Yes, it's very possible. But that's even "dirtier" :) It's more of a "if I can" question than anything else :) – Koval Mar 23 '17 at 21:33
  • *"reference a variable that was constructed based on that constructor function."* Whenever you want to look a value up by name, add that value to a **map**: `var allObjects = new Map(); var someObject = new someConstructor(); allObjects.set('someIdentifier', someObject);`. Then you can look up the value with the `data-*` attribute of the element in the map: `map.get(nameFromDataAttribute)`. That's what I meant with my earlier comment: The business logic of your program should not depend/rely on the variable names you are using. – Felix Kling Mar 25 '17 at 19:21

0 Answers0