Objects.getConstructor()
Syntax
function getConstructor<T>(object: T): Constructor<T> | undefined
Gets the constructor of an object.
Parameters | |
object
|
The object to retrieve the constructor of. |
Example
import { Objects } from 'potence';
const date = new Date();
const constructor = Objects.getConstructor(date);
const date2 = new constructor();
Remarks
In JavaScript, all values* have a retrievable constructor, even primitives
like numbers and strings. When working with constructors in a generic manner,
retrieving these primitive constructors is usually not what you want, so the
following object types will return undefined
instead of their constructor:
- All primitives (including numbers, strings, big ints, etc.)
- Objects whose immediate prototype is
Object
(object literals) - Arrays whose immediate prototype is
Array
(excluding custom arrays)
If you’d like to obtain those constructors as well, use the
constructor
property.
* With the exception of objects created using Object.create(null)