Object - JavaScript MDN
Object - JavaScript MDN
Object
Description
Nearly all objects in JavaScript are instances of
Object ; a typical object inherits properties
(including methods) from Object.prototype ,
although these properties may be shadowed
(a.k.a. overridden). The only objects that don't
inherit from Object.prototype are those with
null prototype, or descended from other null
prototype objects.
__defineGetter__() , __defineSetter__() ,
__lookupGetter__() , and
__lookupSetter__() are deprecated and
should not be used. Use the static
alternatives Object.defineProperty() and
Object.getOwnPropertyDescriptor()
instead.
JS
const obj = {
foo: 1,
// You should not define such a
method on your own object,
// but you may not be able to
prevent it from happening if
// you are receiving the object from
external input
propertyIsEnumerable() {
return false;
},
};
obj.propertyIsEnumerable("foo"); //
false; unexpected result
Object.prototype.propertyIsEnumerable.
call(obj, "foo"); // true; expected
result
null-prototype objects
Almost all objects in JavaScript ultimately
inherit from Object.prototype (see inheritance
and the prototype chain). However, you may
create null -prototype objects using
Object.create(null) or the object initializer
syntax with __proto__: null (note: the
__proto__ key in object literals is different from
the deprecated Object.prototype.__proto__
property). You can also change the prototype
of an existing object to null by calling
Object.setPrototypeOf(obj, null) .
JS
JS
console.log(`normalObj is:
${normalObj}`); // shows "normalObj
is: [object Object]"
console.log(`nullProtoObj is:
${nullProtoObj}`); // throws error:
Cannot convert object to primitive
value
JS
normalObj.valueOf(); // shows {}
nullProtoObj.valueOf(); // throws
error: nullProtoObj.valueOf is not a
function
normalObj.hasOwnProperty("p"); //
shows "true"
nullProtoObj.hasOwnProperty("p"); //
throws error:
nullProtoObj.hasOwnProperty is not a
function
normalObj.constructor; // shows
"Object() { [native code] }"
nullProtoObj.constructor; // shows
"undefined"
JS
nullProtoObj.toString =
Object.prototype.toString; // since
new object lacks toString, add the
original generic one back
console.log(nullProtoObj.toString());
// shows "[object Object]"
console.log(`nullProtoObj is:
${nullProtoObj}`); // shows
"nullProtoObj is: [object Object]"
JS
function hasPerson(name) {
return name in ages;
}
function getAge(name) {
return ages[name];
}
hasPerson("hasOwnProperty"); // true
getAge("toString"); // [Function:
toString]
JS
hasPerson("hasOwnProperty"); // false
getAge("toString"); // undefined
JS
// A malicious script:
Object.prototype.authenticated = true;
// Unexpectedly allowing
unauthenticated user to pass through
if (user.authenticated) {
// access confidential data
}
Array.prototype[@@unscopables] (all
@@unscopables objects should have null -
prototype)
import.meta
Object coercion
Many built-in operations that expect objects
first coerce their arguments to objects. The
operation can be summarized as follows:
Constructor
Object()
Static methods
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.hasOwn()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.seal()
Object.setPrototypeOf()
Object.values()
Instance properties
These properties are defined on
Object.prototype and shared by all Object
instances.
Object.prototype.__proto__
Object.prototype.constructor
Instance methods
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Calls toString() .
Object.prototype.toString()
Object.prototype.valueOf()
Examples