7

Lets say I have

var myObject = {

  'myFunction' : function () {
    // something here that lets me get 'myObject'
  }

}

I have tried various functions found here and stuff like this.constructor.name but i always get 'Object' as the value returned. Is there any way to get the actual name of the variable in this situation?

edit to explain the why so maybe people will better understand...I want to be able to make a function that is continuously called with setInterval. Something like this:

var myObject = {
  'intval' : '',
  'timeout' : 500,

  'start' : function () {
    this.objectName = 'myObject'; // <--I want this to be dynamically popped
    this.intval=window.setInterval("window['"+this.objectName+"'].myFunction()",this.timeout);
  },

  'stop' : function () {
    window.clearInterval(this.intval);
    this.intval='';
  },

  'myFunction' : function () {
    // do something
  }
}

This works just fine if i hardcode 'myObject' to this.objectName but I don't want it to be hardcoded. Problem is that I wan't just do setInterval("window[this.objectName]",100) becausethisis not in the right context whensetInterval` is run, and I don't want to have the objectname hardcoded

3
  • Are you looking for something like this, that is some way to refer to the object? Commented May 31, 2012 at 17:16
  • no, i'm not looking for an object reference, i'm looking for the actual name of the object Commented May 31, 2012 at 17:17
  • For the edited question: start: function () { var that = this; this.intval = setInterval(function () { that.myFunction(); }, this.timeout); }, Commented May 31, 2012 at 17:47

4 Answers 4

5

No, you can't do that in JavaScript.

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

Comments

3

One way;

var myObject = {
    'myFunction' : function () {
        for (var name in window) {
            if (window[name] === this) { 
                alert(name);
                break;
            }
        }
     }
}

Not very nice IMO, marginally better if you wrap it in your own namespace.

You could also always

var myObject = {
    moniker: "myObject",
    ...

In light of your update you don't need to resolve the object at all;

var myObject = {
    'intval' : '',
    'timeout' : 1500,
    'start' : function () {
        var self = this;
        this.intval = window.setInterval(function() {
            self.myFunction()
        }, this.timeout);
    },
    'myFunction' : function() {
        alert(this.intval);
    }
}

3 Comments

yeah i was previously just setting a property like your 2nd example
Updated above, if your prob was the binding of this you don't need to resolve the name
aha! your first example worked fine but that last edit of yours is EXACTLY what I was looking for, thanks!
1
for (var name in this.global)
      if (this.global[name] == this) 
        return name 

How to get class object's name as a string in Javascript?

Comments

1

In short, nope.

The way you're creating your objects, myObject is nothing more than a label with a (relatively loose) affiliation with an object literal. In Java or C++ terms, myObject is more like a pointer then a class name. If you want a stronger link between the label myObject and the object it references, you'll need to use a constructor or subclass pattern. See this article on golimojo.com for a pretty solid overview.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.