The overview

DefineProperty (peson,'name',{value:' Tom ', // describe attribute writable: Get :function(){}, default: undefined set:function(val){}, default: undefined set:function(val){} Default: undefined 64x: Enumerable :true; // Enumerable :true; // Enumerable :true; // Enumerable :true;Copy the code

Writable attribute

Indicates whether the property value can be changed. The default is false.

Object.defineProperty(peson,'name', { value:'tom', }) console.log(peson.name)// Tom // reconfigure Object.defineProperty(peson,'name',{value:'li', } // Re-define property: DefineProperty peson.name = 'li' console.log(peson.name)// Tom //writable is true, DefineProperty (peson,'name',{value:' Tom ', writable: works without any additional information. True}) console.log(peson.name)// Tom // reconfigure Object.defineProperty(peson,'name',{value:'lisa', writable: true }) console.log(peson.name)//lisa peson.name = 'li' console.log(peson.name)//liCopy the code

Get and set

Describes how to assign and value attributes. Default is undefined.

Object.defineProperty(peson,'name',{
    get:function(){
        return temp
    },
    set:function(val){
        temp = val
    }, 
})
peson.name = 'li' 
console.log(peson.name)//li
Copy the code

configurable

Indicates whether the property is configured and whether it can be deleted. Default is false

DefineProperty (peson,'name',{value:' Tom ',}) // Reconfigure Object.defineProperty(peson,'name',{value:'li', } // Re-define property: DefineProperty delete info. Name //Cannot delete property 'name' of #<object> DefineProperty (peson,'name',{value:' Tom ', 64x :true, works without any additional information. }) // reconfigure object.defineProperty (peson,'name',{value:'li', }) console.log(peson.name)//li delete peson.name console.log(peson.name)//undefinedCopy the code

Enumerable properties

Describes whether the property will appear in a for in or object.keys () traversal. The default is false

Obiect.defineProperty (peson, "name ,{ value: "tom", enumerable: true, }); peson.sex ="1"; Object. defineProperty(peson, "age",{ value: 13, enumerable: false, }); console.1og(object.keys(peson)); //["name","sex"] for(let key in peson){ console.1og(key)//name,sex }Copy the code

It’s different from direct assignment

1

DefineProperty (peson,'name',{value:'jie', writable: true, signals: true, enumerable:true, })Copy the code

2

Object.defineproperty (peson,'name',{value:'jie',}) is equivalent to Object.defineProperty(peson,'name',{value:'jie', writable: false, configurable: false, enumerable:false, })Copy the code

Refer to the address: www.jianshu.com/p/8fe1382ba…