Get name of object or class
Get your object's constructor function and then inspect its name property.
myObj.constructor.name
Returns "myClass".
Java - get the current class name?
The "$1" is not "useless non-sense". If your class is anonymous, a number is appended.
If you don't want the class itself, but its declaring class, then you can use getEnclosingClass()
. For example:
Class<?> enclosingClass = getClass().getEnclosingClass();
if (enclosingClass != null) {
System.out.println(enclosingClass.getName());
} else {
System.out.println(getClass().getName());
}
You can move that in some static utility method.
But note that this is not the current class name. The anonymous class is different class than its enclosing class. The case is similar for inner classes.
How to get a JavaScript object's class?
There's no exact counterpart to Java's getClass()
in JavaScript. Mostly that's due to JavaScript being a prototype-based language, as opposed to Java being a class-based one.
Depending on what you need getClass()
for, there are several options in JavaScript:
typeof
instanceof
obj.
constructor
func.
prototype
,proto
.isPrototypeOf
A few examples:
function Foo() {}
var foo = new Foo();
typeof Foo; // == "function"
typeof foo; // == "object"
foo instanceof Foo; // == true
foo.constructor.name; // == "Foo"
Foo.name // == "Foo"
Foo.prototype.isPrototypeOf(foo); // == true
Foo.prototype.bar = function (x) {return x+x;};
foo.bar(21); // == 42
Note: if you are compiling your code with Uglify it will change non-global class names. To prevent this, Uglify has a --mangle
param that you can set to false is using gulp or grunt.
Getting the class name of an instance
Have you tried the __name__
attribute of the class? ie type(x).__name__
will give you the name of the class, which I think is what you want.
>>> import itertools
>>> x = itertools.count(0)
>>> type(x).__name__
'count'
If you're still using Python 2, note that the above method works with new-style classes only (in Python 3+ all classes are "new-style" classes). Your code might use some old-style classes. The following works for both:
x.__class__.__name__
How to determine an object's class?
if (obj instanceof C) {
//your code
}
Get an object's class name at runtime
Simple answer :
class MyClass {}
const instance = new MyClass();
console.log(instance.constructor.name); // MyClass
console.log(MyClass.name); // MyClass
However: beware that the name will likely be different when using minified code.
How to get the object name from within the class?
object1
is just an identifier(or variable) pointing to an instance object, objects don't have names.
>>> class A:
... def foo(self):
... print self
...
>>> a = A()
>>> b = a
>>> c = b
>>> a,b,c #all of them point to the same instance object
(<__main__.A instance at 0xb61ee8ec>, <__main__.A instance at 0xb61ee8ec>, <__main__.A instance at 0xb61ee8ec>)
a
,b
,c
are simply references that allow us to access a same object, when an object has 0 references it is automatically garbage collected.
A quick hack will be to pass the name when creating the instance:
>>> class A:
... def __init__(self, name):
... self.name = name
...
>>> a = A('a')
>>> a.name
'a'
>>> foo = A('foo')
>>> foo.name
'foo'
>>> bar = foo # additional references to an object will still return the original name
>>> bar.name
'foo'
Get the name of a Dart class as a Type or String
The class type can be used as a Type
:
Type myType = MyClass;
how can i get an object name when it is created in java?
If you mean the
name
property, you can't with your code as written. You'd need to either makename
public, or provide a public getter for itIf you mean the name of the class, it would be
book.getClass().getName()
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).
Related Topics
How to Round a Number in JavaScript? .Tofixed() Returns a String
JavaScript Infinitely Looping Slideshow with Delays
How to Create a Web Worker from a String
Why Is Immutability So Important (Or Needed) in JavaScript
How to Access Component Methods from "Outside" in Reactjs
Truncate (Not Round Off) Decimal Numbers in JavaScript
Is It Evil to Use Eval to Convert a String to a Function
JavaScript .Queryselector Find <Div> by Innertext
Correct Way to Handle Conditional Styling in React
Injecting a Mock into an Angularjs Service
What Does the Function Then() Mean in JavaScript
Is There a Sleep Function in JavaScript