Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    marketplace-mod

    Javascript Object methods

    Hey, do you know javascript has some Object methods and you can manipulate your object by using that. We will look into such methods in this blog.

    Before starting, let’s observe master obj (Object) closely

    Please see below result

    As you can see above, some methods are listed like assign, create, freeze, etc so we will look into some of the methods in this blog.

    Object.create()

    This method is used to create new object by using an existing object

    Syntax: Object.create(propertiesObject);

    let’s see another example of create()

    you can check call() method here

    Object.assign()

    it is used to copy one or more source objects into target object.

    Syntax: Object.assign(targetObject, ..sourceObjects);

    what happens if target and source objects have the same properties

    Object.defineProperty()

    You can define an object property by Object.defineProperty()

    Syntax: Object.defineProperty(obj, property, descriptor);

    Let’s see an example

    value: what will be the value of your property

    writable: default it is false which can not be changed, if set true then you can change property

    enumerable: if true then it will display during enumeration

    configurable: default it is false, if true then you can not delete this property

    Set/Get: if using getter and setter then you can not use other keys like writable, enumerable, etc

    You can check getter/setter here

    Object.defineProperties()

    if you want to add multiple properties then you can use it

    Syntax: Object.defineProperties(obj, properties);

    as you can see in the upper example, I have added three properties similar to Object.defineProperty() example

    Object.keys()

    It is used for enumerable keys of an object. You will understand the ‘enumerable’ key here which was used under Object.defineProperty ();

    Syntax: Object.keys(obj)

    let’s see now by removing enumerable key for firstName property and see the following code

    In the upper example, firstName property will not get resulted because enumerable is false for firstName property

    Object.values()

    It is used for getting all enumerable property values of my object not keys

    Syntax: Object.values(obj)

    Object.freeze()

    Freeze method allows us to prevent add/edit/delete new or existing property. If trying to add/edit/delete in strict mode then it will generate errors.

    syntax: Object.freeze(obj)

    if you will run upper code, then you will get result ‘john’ not ‘webkul’ due to frozen

    Object.isFrozen()

    For checking any object is frozen or not

    Syntax: Object.isFrozen(obj)

    Object.seal()

    seal method allows us to prevent add/edit/delete the new or existing property but any existing property can be changed if it is writable. If trying to add/edit/delete any new or existing property in strict mode then it will generate errors except edit in writable case.

    syntax: Object.seal(obj)

    Object.isSealed()

    For checking any object is sealed or not

    syntax: Object.isSealed(obj)

    Object.preventExtensions()

    this method has following features

    prevents to add new or existing property

    edit existing property only if writable

    delete existing property if configurable

    syntax: Object.preventExtensions(obj)

    In the upper example, if you will try to delete ‘lastName’ then you will get an error

    Object.isExtensible()

    For checking any object is extensible or not

    syntax: Object.isExtensible(obj)

    Object.hasOwnProperty()

    For checking any object’s property

    syntax: obj.hasOwnProperty(propertyName)

    That’s it in this blog. I hope this blog was useful to you. Please ask if you have any queries in the comment section.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close