getName
Get the name of an object.
getValue(obj)
Parameters
- obj
{Object}
:The object to get from
Returns
{String}
:
The human-readable name of the object
The @@can.getName symbol is used to provide objects human readable names; the main goal of these names is to help users get a glance of what the object does and what it is used for.
There are no hard rules to define names but CanJS uses the following convention for consistent names across its observable types:
- The name starts with the observable constructor name
- The constructor name is decorated with the following characters based on its type:
<>
: for value-like observables, e.g:SimpleObservable<>
[]
: for list-like observables, e.g:DefineList[]
{}
: for map-like observables, e.g:DefineMap{}
- Any property that makes the instance unique (like ids) are printed inside the chars mentioned before.
The example below shows how to implement @@can.getName, in a value-like observable (similar to can-simple-observable).
var canReflect = require("can-reflect");
function MySimpleObservable(value) {
this.value = value;
}
canReflect.assignSymbols(MySimpleObservable.prototype, {
"can.getName": function() {
//!steal-remove-start
if (process.env.NODE_ENV !== 'production') {
var value = JSON.stringify(this.value);
return canReflect.getName(this.constructor) + "<" + value + ">";
}
//!steal-remove-end
}
});
With that in place, MySimpleObservable
can be used like this:
var one = new MySimpleObservable(1);
canReflect.getName(one); // MySimpleObservable<1>