Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 211837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:07:53+00:00 2026-05-11T18:07:53+00:00

Let’s say I instantiate an object in Javascript like this: var myObj = new

  • 0

Let’s say I instantiate an object in Javascript like this:

var myObj = new someObject();

Now, is it possible to obtain the var object’s name as string 'myObj' from within one of the class methods?


Additional details (edited):

The reason why I would like to get the name of the variable holding reference to the object is that my new myObj would create a new clickable DIV on the page that would need to call a function myObj.someFunction(). As I insert the new DIV I need to know the name of the variable holding reference to the object. Is there maybe a better way of doing this?


You are right, sorry for the mixup in terminology.

The reason why I would like to get the name of the variable holding reference to the object is that my new myObj would create a new clickable DIV on the page that would need to call a function myObj.someFunction(). As I insert the new DIV I need to know the name of the variable holding reference to the object. Is there maybe a better way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:07:53+00:00Added an answer on May 11, 2026 at 6:07 pm

    Shog9 is right that this doesn’t make all that much sense to ask, since an object could be referred to by multiple variables. If you don’t really care about that, and all you want is to find the name of one of the global variables that refers to that object, you could do the following hack:

    function myClass() { 
      this.myName = function () { 
        // search through the global object for a name that resolves to this object
        for (var name in this.global) 
          if (this.global[name] == this) 
            return name 
      } 
    }
    // store the global object, which can be referred to as this at the top level, in a
    // property on our prototype, so we can refer to it in our object's methods
    myClass.prototype.global = this
    // create a global variable referring to an object
    var myVar = new myClass()
    myVar.myName() // returns "myVar"
    

    Note that this is an ugly hack, and should not be used in production code. If there is more than one variable referring to an object, you can’t tell which one you’ll get. It will only search the global variables, so it won’t work if a variable is local to a function. In general, if you need to name something, you should pass the name in to the constructor when you create it.

    edit: To respond to your clarification, if you need to be able to refer to something from an event handler, you shouldn’t be referring to it by name, but instead add a function that refers to the object directly. Here’s a quick example that I whipped up that shows something similar, I think, to what you’re trying to do:

    function myConstructor () {
      this.count = 0
      this.clickme = function () {
        this.count += 1
        alert(this.count)
      }
    
      var newDiv = document.createElement("div")
      var contents = document.createTextNode("Click me!")
    
      // This is the crucial part. We don't construct an onclick handler by creating a
      // string, but instead we pass in a function that does what we want. In order to
      // refer to the object, we can't use this directly (since that will refer to the 
      // div when running event handler), but we create an anonymous function with an 
      // argument and pass this in as that argument.
      newDiv.onclick = (function (obj) { 
        return function () {
          obj.clickme()
        }
      })(this)
    
      newDiv.appendChild(contents)
      document.getElementById("frobnozzle").appendChild(newDiv)
    
    }
    window.onload = function () {
      var myVar = new myConstructor()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 180k
  • Answers 180k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When are you testing this? If you test the code… May 12, 2026 at 3:59 pm
  • Editorial Team
    Editorial Team added an answer EDIT: I found an article on CodeProject with sample code… May 12, 2026 at 3:59 pm
  • Editorial Team
    Editorial Team added an answer Just use \1 ... \9 (or $1 ... $9 in… May 12, 2026 at 3:59 pm

Related Questions

I have a French site that I want to parse, but am running into
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.