with example
Here are examples with explanations for key JavaScript Object static methods and related
instance methods to clarify their usage:
Static Methods with Examples
[Link](target, ...sources)
Copies enumerable own properties from source objects to a target object (merges objects).
const target = { a: 1 };
const source = { b: 2 };
[Link](target, source);
[Link](target); // { a: 1, b: 2 }
[Link](proto, propertiesObject?)
Creates a new object with the specified prototype object.
const proto = { greet() { return "hello"; } };
const obj = [Link](proto);
[Link]([Link]()); // "hello"
[Link](obj, props)
Adds multiple properties with detailed descriptors at once.
const obj = {};
[Link](obj, {
prop1: { value: 42, writable: true },
prop2: { value: 'hello', enumerable: true }
});
[Link](obj.prop1); // 42
[Link](obj)
Returns an array of [key, value] pairs for own enumerable properties.
const obj = { a: 1, b: 2 };
[Link]([Link](obj)); // [ ['a', 1], ['b', 2] ]
[Link](obj)
Prevents adding, removing, or changing properties (makes object immutable).
const obj = { a: 1 };
[Link](obj);
obj.a = 2;
[Link](obj.a); // 1 (unchanged)
[Link](iterable)
Creates an object from an iterable of key-value pairs (inverse of entries()).
const entries = [['a', 1], ['b', 2]];
const obj = [Link](entries);
[Link](obj); // { a: 1, b: 2 }
[Link](obj, prop)
Returns property descriptor for a specific property.
const obj = { a: 1 };
[Link]([Link](obj, 'a'));
// { value: 1, writable: true, enumerable: true, configurable: true }
[Link](obj)
Returns an array of own symbol properties.
const sym = Symbol('foo');
const obj = { [sym]: 123 };
[Link]([Link](obj)); // [ Symbol(foo) ]
[Link](obj, prop)
Checks if property exists directly on the object.
const obj = { a: 1 };
[Link]([Link](obj, 'a')); // true
[Link]([Link](obj, 'b')); // false
[Link](value1, value2)
Compares two values for exact equality (like === but treats NaN equal to NaN and
distinguishes +0 and -0).
[Link]([Link]('foo', 'foo')); // true
[Link]([Link](NaN, NaN)); // true
[Link]([Link](0, -0)); // false
[Link](obj)
Returns an array of own enumerable property names.
const obj = { a: 1, b: 2 };
[Link]([Link](obj)); // ['a', 'b']
[Link](obj)
Returns an array of own enumerable property values.
const obj = { a: 1, b: 2 };
[Link]([Link](obj)); // [1, 2]
[Link](obj)
Prevents addition or removal of properties, existing properties can still be modified if
writable.
const obj = { a: 1 };
[Link](obj);
obj.a = 2;
delete obj.a; // fails silently or throws in strict mode
[Link](obj.a); // 2 (modified but not deleted)
[Link](obj, proto)
Sets the prototype (internal [[Prototype]]) of an object.
const proto = { greet() { return "hi"; } };
const obj = {};
[Link](obj, proto);
[Link]([Link]()); // "hi"
Instance Methods with Examples
[Link](prop)
Returns true if the object has the specified property as own property.
const obj = { a: 1 };
[Link]([Link]('a')); // true
[Link](obj2)
Returns true if this object exists in the prototype chain of obj2.
const proto = {};
const obj = [Link](proto);
[Link]([Link](obj)); // true
[Link](prop)
Returns true if property is enumerable.
const obj = { a: 1 };
[Link]([Link]('a')); // true
[Link]()
Returns a string representation, usually "[object Object]" unless overridden.
const obj = {};
[Link]([Link]()); // "[object Object]"
[Link]()
Returns the primitive value of the object or the object itself.
const obj = {};
[Link]([Link]() === obj); // true
If you want more examples for other specific static or instance methods, or for Function
methods/inheritance, please ask!
⁂