how to use javascript Object.defineProperty

前端 未结 10 1156
不知归路
不知归路 2020-11-29 14:21

I looked around for how to use the Object.defineProperty method, but couldn\'t find anything decent.

Someone gave me this snippet of code:

Object.def         


        
10条回答
  •  独厮守ぢ
    2020-11-29 14:45

    defineProperty is a method on Object which allow you to configure the properties to meet some criterias. Here is a simple example with an employee object with two properties firstName & lastName and append the two properties by overriding the toString method on the object.

    var employee = {
        firstName: "Jameel",
        lastName: "Moideen"
    };
    employee.toString=function () {
        return this.firstName + " " + this.lastName;
    };
    console.log(employee.toString());
    

    You will get Output as : Jameel Moideen

    I am going to change the same code by using defineProperty on the object

    var employee = {
        firstName: "Jameel",
        lastName: "Moideen"
    };
    Object.defineProperty(employee, 'toString', {
        value: function () {
            return this.firstName + " " + this.lastName;
        },
        writable: true,
        enumerable: true,
        configurable: true
    });
    console.log(employee.toString());
    

    The first parameter is the name of the object and then second parameter is name of the property we are adding , in our case it’s toString and then the last parameter is json object which have a value going to be a function and three parameters writable,enumerable and configurable.Right now I just declared everything as true.

    If u run the example you will get Output as : Jameel Moideen

    Let’s understand why we need the three properties such as writable,enumerable and configurable.

    writable

    One of the very annoying part of the javascript is , if you change the toString property to something else for example

    if you run this again , everything gets breaks. Let’s change writable to false. If run the same again you will get the correct output as ‘Jameel Moideen’ . This property will prevent overwrite this property later.

    enumerable

    if you print all the keys inside the object , you can see all the properties including toString.

    console.log(Object.keys(employee));
    

    if you set enumerable to false , you can hide toString property from everybody else. If run this again you will get firstName,lastName

    configurable

    if someone later redefined the object on later for example enumerable to true and run it. You can see toString property came again.

    var employee = {
        firstName: "Jameel",
        lastName: "Moideen"
    };
    Object.defineProperty(employee, 'toString', {
        value: function () {
            return this.firstName + " " + this.lastName;
        },
        writable: false,
        enumerable: false,
        configurable: true
    });
    
    //change enumerable to false
    Object.defineProperty(employee, 'toString', {
    
        enumerable: true
    });
    employee.toString="changed";
    console.log(Object.keys(employee));
    

    you can restrict this behavior by set configurable to false.

    Orginal reference of this information is from my personal Blog

提交回复
热议问题