Imagine you have an object and you're looking to retrieve a string representing the name of the property you're accessing. Does that particular operation have a designated term? It seems like a form of reflection
, but is there a more precise term for it, and do programming languages incorporate this concept in a standardized manner?
For instance, this functionality is present for class-based objects through the use of toString()
in languages like JavaScript or ActionScript.
var person:Person = new Person();
var nameOfClass:String = person.toString();
console.log(nameOfClass); // outputs "Person" or "com.mydomain.model.Person"
However, as properties can be null or undefined, using toString()
may result in an error or return the actual value:
var person:Person = new Person();
var nameOfProperty:String = person.name.toString(); // errors out if null
person.name = "John";
person.name.toString(); // returns "John" instead of the type
Therefore, it's likely that certain programming languages feature a global function along these lines:
var person:Person = new Person();
var nameOfProperty:String = getPropertyName(person.name);
console.log(nameOfProperty); // outputs "name"
nameOfProperty = getPropertyName(person.zipCode);
console.log(nameOfProperty); // outputs "zipCode"
Does this specific functionality have a recognized name, and if so, how is it typically implemented?