JavaScript - object methods

revision:


list of methods and decription

Object.assign() : copies the values of all enumerable own properties from one or more source objects to a target object.

Object.create() : creates a new object with the specified prototype object and properties.

Object.defineProperties() : adds the named properties described by the given descriptors to an object.

Object.defineProperty() : adds the named property described by a given descriptor to an object.

Object.entries() : returns an array containing all of the [key, value] pairs of a given object's own enumerable string properties.

Object.freeze() : freezes an object. Other code cannot delete or change its properties.

Object.fromEntries() : returns a new object from an iterable of [key, value] pairs. (This is the reverse of Object.entries).

Object.getOwnPropertyDescriptor() : returns a property descriptor for a named property on an object.

Object.getOwnPropertyDescriptors() : returns an object containing all own property descriptors for an object.

Object.getOwnPropertyNames() : returns an array containing the names of all of the given object's own enumerable and non-enumerable properties.

Object.getOwnPropertySymbols() : returns an array of all symbol properties found directly upon a given object.

Object.getPrototypeOf() : returns the prototype (internal [[Prototype]] property) of the specified object.

Object.groupBy() : groups the elements of a given iterable according to the string values returned by a provided callback function. The returned object has separate properties for each group, containing arrays with the elements in the group.

Object.hasOwn() : returns true if the specified object has the indicated property as its own property, or false if the property is inherited or does not exist.

Object.is() : compares if two values are the same value. Equates all NaN values (which differs from both IsLooselyEqual used by == and IsStrictlyEqual used by ===).

Object.isExtensible() : determines if extending of an object is allowed.

Object.isFrozen() : determines if an object was frozen.

Object.isSealed() : determines if an object is sealed.

Object.keys() : returns an array containing the names of all of the given object's own enumerable string properties.

Object.preventExtensions() : prevents any extensions of an object.

Object.seal() : prevents other code from deleting properties of an object.

Object.setPrototypeOf() : sets the object's prototype (its internal [[Prototype]] property).

Object.values() : returns an array containing the values that correspond to all of a given object's own enumerable string properties.


examples:

code:
                <div>
                    <p id="object1"></p>
                    <p id="object2"></p>
                    <p id="object3"></p>
                    <p id="object4"></p>
                    <p id="object5"></p>
        
                </div>
                <script>
                    const object1 = {
                        name: 'John',
                        age: 20,
        
                    };
                    document.getElementById("object1").innerHTML = "keys(): " + JSON.stringify(Object.keys(object1));
                    document.getElementById("object2").innerHTML = "values(): " + JSON.stringify(Object.values(object1));
        
                    const personA = {
                        name: "Oscar",
                        introduction: function() {
                            document.getElementById("object3").innerHTML = "create() : " + `my name is ${this.name}`;
                            console.log("create(): " +`My name is ${this.name}`);
                        }
                        };
                        const me = Object.create(personA);
                        me.name = 'Theo'; 
                        me.introduction();
        
                        Object.freeze(personA);
                        personA.name = "Filip";
                        console.log("freeze(): ", personA.name)
                        document.getElementById("object4").innerHTML = "freeze() : " + personA.name;
        
                        const personB = {
                            name: "Filip",
                            age : 20
                        };
        
                        const obj2 = {
                            ishuman: true
        
                        }
                        Object.assign(personB, obj2);
                        console.log(personB);
                        document.getElementById("object5").innerHTML = "assign: " + JSON.stringify(personB);
                </script>