Grammar
Object.defineproperty (obj, prop, descriptor)
Parameter description:
OBJ: Required. Target Object
Prop: Required. The name of the property that needs to be defined or modified
Descriptor: Required. Attributes owned by the target property
return value:
The object passed into the function. That is, the first parameter, obj.
For attributes, we can set some properties for this property, such as whether it is read-only or not, and whether it can be used for . in or Object.keys () traversal.
Example:
<!DOCTYPE HTML><HTMLLang= "zh"> <Head> <MetaCharSet= "UTF-8" /> <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0" /> <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge" /> <title>Object.defineproperty Example</title> </Head> <Body> <ul> <LiID= "First">1</Li> <Li>2</Li> <Li>3</Li> </ul> <Scripttype= "Text/javascript"> varSS={age:Ten }; varname= 'Mfg'; Object.defineproperty (SS,'name', { //sets whether it is possible to enumerateEnumerable:false, //Whether you can delete the target property or modify the property againConfigurable:false, //Get Property valueget () {returnname; }, //Setting property valuesSet (val) {name=Val; }}) Console.log (ss.name) ss.name= 'New Value'; Console.log (Ss.name); </Script> </Body></HTML>
Ref: 1190000007434923
JS Object.defineproperty Use