In JavaScript, there is a bit of a mixture between utility methods and enums existing as inherited prototype properties as opposed to utility methods and enums that only exist on the built-in object itself.
For example, Date
is a constructable object, but constructed Date
instances that inherit the built-in Date
prototype object's properties will not have the now
property. The now
property only exists on (and is only callable from) the global Date
object.
const aNewDate = new Date()
Date.now() // fine
aNewDate.now() // TypeError
If we construct a new object from a built-in object, we won't be able to directly see the properties that the object inherits from the original built-in object's prototype. For example:
// create a new array, which should inherit properties from the Array prototype
const arr = [1,2,3]
// if we try to log the properties of arr, we don't see the prototype properties
console.log(Object.getOwnPropertyNames(arr))
// output: [ '0', '1', '2', 'length' ]
// this, of course, works, even though the reverse() property doesn't show up when
// calling Object.getOwnPropertyNames(arr)
arr.reverse() // -> [3,2,1]
This usually isn't an issue since we generally know the type of an object (or we can use typeof
to get this information), and we can infer what utility properties are available on the object. But, we can print an object's own properties as well as its prototype properties side by side, anyway:
const arr = [1,2,3]
console.log({
props: Object.getOwnPropertyNames(arr),
prototypeProps: Object.getOwnPropertyNames(Object.getPrototypeOf(arr)),
})
/* output:
{ props: [ '0', '1', '2', 'length' ],
prototypeProps:
[ 'length',
'constructor',
'concat',
'copyWithin',
'fill',
'find',
'findIndex',
'lastIndexOf',
'pop',
'push',
'reverse',
'shift',
'unshift',
'slice',
'sort',
'splice',
'includes',
'indexOf',
'join',
'keys',
'entries',
'values',
'forEach',
'filter',
'flat',
'flatMap',
'map',
'every',
'some',
'reduce',
'reduceRight',
'toLocaleString',
'toString' ] }
*/
Below is the logged output of using Object.getOwnPropertyNames()
on a variety of common JavaScript built-ins as well as their prototypes as they exist in an ESNext 2020 context. We can see that some properties exist solely on the built-in object itself, while others will be available on the object's prototype and will, by association, be available on any instances of that object prototype.
Built-ins logged:
- Array
- Boolean
- Date
- Error
- Function
- JSON
- Math
- Number
- Object
- Promise
- RegExp
- String
- Symbol
// common built-in objects and their prototypes
console.log({
ArrayProperties: Object.getOwnPropertyNames(Array),
ArrayPrototypeProperties: Object.getOwnPropertyNames(Array.prototype),
BooleanProperties: Object.getOwnPropertyNames(Boolean),
BooleanPrototypeProperties: Object.getOwnPropertyNames(Boolean.prototype),
DateProperties: Object.getOwnPropertyNames(Date),
DatePrototypeProperties: Object.getOwnPropertyNames(Date.prototype),
ErrorProperties: Object.getOwnPropertyNames(Error),
ErrorPrototypeProperties: Object.getOwnPropertyNames(Error.prototype),
FunctionProperties: Object.getOwnPropertyNames(Function),
FunctionPrototypeProperties: Object.getOwnPropertyNames(Function.prototype),
// this built-in object doesn't have a prototype and is therefore not constructable
JSONProperties: Object.getOwnPropertyNames(JSON),
// this built-in object doesn't have a prototype and is therefore not constructable
MathProperties: Object.getOwnPropertyNames(Math),
NumberProperties: Object.getOwnPropertyNames(Number),
NumberPrototypeProperties: Object.getOwnPropertyNames(Number.prototype),
ObjectProperties: Object.getOwnPropertyNames(Object),
ObjectPrototypeProperties: Object.getOwnPropertyNames(Object.prototype),
PromiseProperties: Object.getOwnPropertyNames(Promise),
PromisePrototypeProperties: Object.getOwnPropertyNames(Promise.prototype),
RegExpProperties: Object.getOwnPropertyNames(RegExp),
RegExpPrototypeProperties: Object.getOwnPropertyNames(RegExp.prototype),
StringProperties: Object.getOwnPropertyNames(String),
StringPrototypeProperties: Object.getOwnPropertyNames(String.prototype),
SymbolProperties: Object.getOwnPropertyNames(Symbol),
SymbolPrototypeProperties: Object.getOwnPropertyNames(Symbol.prototype),
})
Output:
{ ArrayProperties: [ 'length', 'name', 'prototype', 'isArray', 'from', 'of' ],
ArrayPrototypeProperties:
[ 'length',
'constructor',
'concat',
'copyWithin',
'fill',
'find',
'findIndex',
'lastIndexOf',
'pop',
'push',
'reverse',
'shift',
'unshift',
'slice',
'sort',
'splice',
'includes',
'indexOf',
'join',
'keys',
'entries',
'values',
'forEach',
'filter',
'flat',
'flatMap',
'map',
'every',
'some',
'reduce',
'reduceRight',
'toLocaleString',
'toString' ],
BooleanProperties: [ 'length', 'name', 'prototype' ],
BooleanPrototypeProperties: [ 'constructor', 'toString', 'valueOf' ],
DateProperties: [ 'length', 'name', 'prototype', 'now', 'parse', 'UTC' ],
DatePrototypeProperties:
[ 'constructor',
'toString',
'toDateString',
'toTimeString',
'toISOString',
'toUTCString',
'toGMTString',
'getDate',
'setDate',
'getDay',
'getFullYear',
'setFullYear',
'getHours',
'setHours',
'getMilliseconds',
'setMilliseconds',
'getMinutes',
'setMinutes',
'getMonth',
'setMonth',
'getSeconds',
'setSeconds',
'getTime',
'setTime',
'getTimezoneOffset',
'getUTCDate',
'setUTCDate',
'getUTCDay',
'getUTCFullYear',
'setUTCFullYear',
'getUTCHours',
'setUTCHours',
'getUTCMilliseconds',
'setUTCMilliseconds',
'getUTCMinutes',
'setUTCMinutes',
'getUTCMonth',
'setUTCMonth',
'getUTCSeconds',
'setUTCSeconds',
'valueOf',
'getYear',
'setYear',
'toJSON',
'toLocaleString',
'toLocaleDateString',
'toLocaleTimeString' ],
ErrorProperties:
[ 'length',
'name',
'prototype',
'captureStackTrace',
'stackTraceLimit' ],
ErrorPrototypeProperties: [ 'constructor', 'name', 'message', 'toString' ],
FunctionProperties: [ 'length', 'name', 'prototype' ],
FunctionPrototypeProperties:
[ 'length',
'name',
'arguments',
'caller',
'constructor',
'apply',
'bind',
'call',
'toString' ],
JSONProperties: [ 'parse', 'stringify' ],
MathProperties:
[ 'abs',
'acos',
'acosh',
'asin',
'asinh',
'atan',
'atanh',
'atan2',
'ceil',
'cbrt',
'expm1',
'clz32',
'cos',
'cosh',
'exp',
'floor',
'fround',
'hypot',
'imul',
'log',
'log1p',
'log2',
'log10',
'max',
'min',
'pow',
'random',
'round',
'sign',
'sin',
'sinh',
'sqrt',
'tan',
'tanh',
'trunc',
'E',
'LN10',
'LN2',
'LOG10E',
'LOG2E',
'PI',
'SQRT1_2',
'SQRT2' ],
NumberProperties:
[ 'length',
'name',
'prototype',
'isFinite',
'isInteger',
'isNaN',
'isSafeInteger',
'parseFloat',
'parseInt',
'MAX_VALUE',
'MIN_VALUE',
'NaN',
'NEGATIVE_INFINITY',
'POSITIVE_INFINITY',
'MAX_SAFE_INTEGER',
'MIN_SAFE_INTEGER',
'EPSILON' ],
NumberPrototypeProperties:
[ 'constructor',
'toExponential',
'toFixed',
'toPrecision',
'toString',
'valueOf',
'toLocaleString' ],
ObjectProperties:
[ 'length',
'name',
'prototype',
'assign',
'getOwnPropertyDescriptor',
'getOwnPropertyDescriptors',
'getOwnPropertyNames',
'getOwnPropertySymbols',
'is',
'preventExtensions',
'seal',
'create',
'defineProperties',
'defineProperty',
'freeze',
'getPrototypeOf',
'setPrototypeOf',
'isExtensible',
'isFrozen',
'isSealed',
'keys',
'entries',
'fromEntries',
'values' ],
ObjectPrototypeProperties:
[ 'constructor',
'__defineGetter__',
'__defineSetter__',
'hasOwnProperty',
'__lookupGetter__',
'__lookupSetter__',
'isPrototypeOf',
'propertyIsEnumerable',
'toString',
'valueOf',
'__proto__',
'toLocaleString' ],
PromiseProperties:
[ 'length',
'name',
'prototype',
'all',
'race',
'resolve',
'reject',
'allSettled',
'any' ],
PromisePrototypeProperties: [ 'constructor', 'then', 'catch', 'finally' ],
RegExpProperties:
[ 'length',
'name',
'prototype',
'input',
'$_',
'lastMatch',
'$&',
'lastParen',
'$+',
'leftContext',
'$`',
'rightContext',
'$\'',
'$1',
'$2',
'$3',
'$4',
'$5',
'$6',
'$7',
'$8',
'$9' ],
RegExpPrototypeProperties:
[ 'constructor',
'exec',
'dotAll',
'flags',
'global',
'ignoreCase',
'multiline',
'source',
'sticky',
'unicode',
'compile',
'toString',
'test' ],
StringProperties:
[ 'length',
'name',
'prototype',
'fromCharCode',
'fromCodePoint',
'raw' ],
StringPrototypeProperties:
[ 'length',
'constructor',
'anchor',
'big',
'blink',
'bold',
'charAt',
'charCodeAt',
'codePointAt',
'concat',
'endsWith',
'fontcolor',
'fontsize',
'fixed',
'includes',
'indexOf',
'italics',
'lastIndexOf',
'link',
'localeCompare',
'match',
'matchAll',
'normalize',
'padEnd',
'padStart',
'repeat',
'replace',
'search',
'slice',
'small',
'split',
'strike',
'sub',
'substr',
'substring',
'sup',
'startsWith',
'toString',
'trim',
'trimStart',
'trimLeft',
'trimEnd',
'trimRight',
'toLocaleLowerCase',
'toLocaleUpperCase',
'toLowerCase',
'toUpperCase',
'valueOf',
'replaceAll' ],
SymbolProperties:
[ 'length',
'name',
'prototype',
'for',
'keyFor',
'asyncIterator',
'hasInstance',
'isConcatSpreadable',
'iterator',
'match',
'matchAll',
'replace',
'search',
'species',
'split',
'toPrimitive',
'toStringTag',
'unscopables' ],
SymbolPrototypeProperties: [ 'constructor', 'toString', 'valueOf', 'description' ] }