0% found this document useful (0 votes)
7 views4 pages

With Example

The document provides examples and explanations for key JavaScript Object static and instance methods. It covers methods such as Object.assign, Object.create, and Object.freeze, along with their usage in code snippets. Additionally, it includes instance methods like hasOwnProperty and isPrototypeOf, demonstrating how to interact with object properties and prototypes.

Uploaded by

Harsh Rawani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

With Example

The document provides examples and explanations for key JavaScript Object static and instance methods. It covers methods such as Object.assign, Object.create, and Object.freeze, along with their usage in code snippets. Additionally, it includes instance methods like hasOwnProperty and isPrototypeOf, demonstrating how to interact with object properties and prototypes.

Uploaded by

Harsh Rawani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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


Object.assign(target, ...sources)
Copies enumerable own properties from source objects to a target object (merges objects).

const target = { a: 1 };
const source = { b: 2 };
Object.assign(target, source);
console.log(target); // { a: 1, b: 2 }

Object.create(proto, propertiesObject?)
Creates a new object with the specified prototype object.

const proto = { greet() { return "hello"; } };


const obj = Object.create(proto);
console.log(obj.greet()); // "hello"

Object.defineProperties(obj, props)
Adds multiple properties with detailed descriptors at once.

const obj = {};


Object.defineProperties(obj, {
prop1: { value: 42, writable: true },
prop2: { value: 'hello', enumerable: true }
});
console.log(obj.prop1); // 42

Object.entries(obj)
Returns an array of [key, value] pairs for own enumerable properties.

const obj = { a: 1, b: 2 };
console.log(Object.entries(obj)); // [ ['a', 1], ['b', 2] ]

Object.freeze(obj)
Prevents adding, removing, or changing properties (makes object immutable).

const obj = { a: 1 };
Object.freeze(obj);
obj.a = 2;
console.log(obj.a); // 1 (unchanged)

Object.fromEntries(iterable)
Creates an object from an iterable of key-value pairs (inverse of entries()).

const entries = [['a', 1], ['b', 2]];


const obj = Object.fromEntries(entries);
console.log(obj); // { a: 1, b: 2 }

Object.getOwnPropertyDescriptor(obj, prop)
Returns property descriptor for a specific property.

const obj = { a: 1 };
console.log(Object.getOwnPropertyDescriptor(obj, 'a'));
// { value: 1, writable: true, enumerable: true, configurable: true }

Object.getOwnPropertySymbols(obj)
Returns an array of own symbol properties.

const sym = Symbol('foo');


const obj = { [sym]: 123 };
console.log(Object.getOwnPropertySymbols(obj)); // [ Symbol(foo) ]

Object.hasOwn(obj, prop)
Checks if property exists directly on the object.

const obj = { a: 1 };
console.log(Object.hasOwn(obj, 'a')); // true
console.log(Object.hasOwn(obj, 'b')); // false

Object.is(value1, value2)
Compares two values for exact equality (like === but treats NaN equal to NaN and
distinguishes +0 and -0).

console.log(Object.is('foo', 'foo')); // true


console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0)); // false

Object.keys(obj)
Returns an array of own enumerable property names.

const obj = { a: 1, b: 2 };
console.log(Object.keys(obj)); // ['a', 'b']

Object.values(obj)
Returns an array of own enumerable property values.
const obj = { a: 1, b: 2 };
console.log(Object.values(obj)); // [1, 2]

Object.seal(obj)
Prevents addition or removal of properties, existing properties can still be modified if
writable.

const obj = { a: 1 };
Object.seal(obj);
obj.a = 2;
delete obj.a; // fails silently or throws in strict mode
console.log(obj.a); // 2 (modified but not deleted)

Object.setPrototypeOf(obj, proto)
Sets the prototype (internal [[Prototype]]) of an object.

const proto = { greet() { return "hi"; } };


const obj = {};
Object.setPrototypeOf(obj, proto);
console.log(obj.greet()); // "hi"

Instance Methods with Examples


obj.hasOwnProperty(prop)
Returns true if the object has the specified property as own property.

const obj = { a: 1 };
console.log(obj.hasOwnProperty('a')); // true

obj.isPrototypeOf(obj2)
Returns true if this object exists in the prototype chain of obj2.

const proto = {};


const obj = Object.create(proto);
console.log(proto.isPrototypeOf(obj)); // true

obj.propertyIsEnumerable(prop)
Returns true if property is enumerable.

const obj = { a: 1 };
console.log(obj.propertyIsEnumerable('a')); // true

obj.toString()
Returns a string representation, usually "[object Object]" unless overridden.

const obj = {};


console.log(obj.toString()); // "[object Object]"
obj.valueOf()
Returns the primitive value of the object or the object itself.

const obj = {};


console.log(obj.valueOf() === obj); // true

If you want more examples for other specific static or instance methods, or for Function
methods/inheritance, please ask!

You might also like