0

I want to be able to access the var name of this class instance from inside a method.. Can I set the actual variable name as a string on the constructor or something?

var myClass = function(filePath){
    this.run = function(){
        //  I want to access the string "x"
        console.log( this.variableName ); // ????

    };
};

var x = new myClass("./sayHi.js");
3
  • 4
    Not possible, why would you even want that? You can access your instance using the this keyword. And you should define your methods on the prototype of the constructor not inside it. Commented Mar 12, 2015 at 3:21
  • 1
    I don' quite understand what you want to do. Commented Mar 12, 2015 at 3:21
  • from what I gather, the intent is for x.run() to print out "x". I do not think this is possible and agree with @LJ_1102, I am not sure why you would want to do this. Commented Mar 12, 2015 at 3:26

1 Answer 1

-1

Pass the variable name as a parameter when you construct it

var myClass = function(assigned_name,filePath){

    this.variableName = assigned_name

    this.run = function(){
        console.log( this.variableName );
    }
}

var x = new myClass('x','./sayHi.js');

x.run()

Output:

'x'

Useful for:

While many people say this is pointless, I have run into a situation where I need it. And a side-note, many jQuery operations rely on the principle of knowing what its own variable name is. It is always '$'. And people refer to '$' from inside a jQuery method when assigning anonymous functions to onclick events without realizing that it is exactly what your question is asking for.

You will need this if your myClass() object dynamically assigns event handlers that call-back to your specific class instance, as these calls are often reliant on information saved in your specific class variable. Since your myClass() function isn't as wide-spread as jQuery, you can't count on it always being assigned to the same variable, like '$'.

Passing its own assigned variable name to itself when it is constructed is the best way I've found to let my class instance know what its global reference is.

Sign up to request clarification or add additional context in comments.

3 Comments

jQuery is a factory function not an instance, from within jQuery $ is scoped so you can name jQuery what ever you want, there is a noConflict mode that leaves $ unassigned. There is absolutely no good reason for knowing the instances variable name from within the instance as this is a clear violation of clean programming principles, its not within the responsibility of the instance to know what the outer code is doing. What if i push the instance into an array, or dont even assign it to a variable?
Thank you but, I am really trying to avoid passing constructor an object {path:"myWebWorker.js", varInstanceName : "newWorker"}
any help would be useful, with this, moldable language console.log = function(msg){ alert(msg); };

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.