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:
- This would reduce code complexity in my case
- I'm just wandering if it's possible :)