How to Get the Name of an Object

Get name of object or class

Get your object's constructor function and then inspect its name property.

myObj.constructor.name

Returns "myClass".

How to get the object name of an object in JavaScript

You cannot.*

First of all, more than one variables can point to the same object. Variables are only references, not containers. There is no one true name for an object. Here's an example:

function makeExample() () {
var x = {startIndex: 1, stopIndex: 2};
var y = x;
return y;
}

var z = makeExample();

In the above code, what should the name of the object be? x, y, z? All of these variables don't contain a copy of the object, they point to the same object. exampleObject is the name of the variable, not the name of the object.

A variable name is just a label to be used by the programmer, not by code. That is not the same thing as a property, which is data stored inside an object and identified by a key which is either a string or a symbol. If the object needs to have a name, then it should be part of its own properties:

var exampleObject = { name: "exampleObject" };

*Technically, any variable created with var in the global scope of a script that is not executed as a module will be added to the window object. That is a relic of the past and you should not rely on this - in fact, modern JS code should use let to create variables which do not have this behavior.

Getting the object's property name

Use Object.keys():

var myObject = { a: 'c', b: 'a', c: 'b' };var keyNames = Object.keys(myObject);console.log(keyNames); // Outputs ["a","b","c"]

how to get object's name in javascript?

You can't. You're only passing the object { black : 'bad', gray : 'not so bad' } into test. This object does not intrinsically have the name "dark", it's just an object that happened to exist as the property dark of the object a. This information is irretrievably lost when passing it into a function.

You're basically trying to retrieve the variable name that held the value before the value got passed into the function. That's not possible.

how can i get an object name when it is created in java?

  1. If you mean the name property, you can't with your code as written. You'd need to either make name public, or provide a public getter for it

  2. If you mean the name of the class, it would be

    book.getClass().getName()
  3. If you mean the name of the variable you've assigned it to (book), you can't, that isn't information available at runtime (outside of a debug build and debugger introspection).

How can I get the name of an object?

Objects do not necessarily have names in Python, so you can't get the name.

It's not unusual for objects to have a __name__ attribute in those cases that they do have a name, but this is not a part of standard Python, and most built in types do not have one.

When you create a variable, like the x, y, z above then those names just act as "pointers" or "references" to the objects. The object itself does not know what name you are using for it, and you can not easily (if at all) get the names of all references to that object.

Update: However, functions do have a __name__ (unless they are lambdas) so, in that case you can do:

dict([(t.__name__, t) for t in fun_list])

How to get the property name of object and use that property name in javascript

The only error in your code is mapped_errors.{obj_key}.msg. You should be using [] to access the object's property value when you are using a variable to get the key name, as mapped_errors[obj_key].