pk10赛车计划软件百度云

Fuzhou website construction only needs 1,000 yuan to include domain name space, details 18558752805

  • understanding of object.defineProperty

Fuzhou Website Construction > Site News > WeChat Mini Program >

Source: http://www.jianshu.com/p/5cad911a3630

In JavaScript, there are several ways to define properties of objects:

var personObj = { } ; personObj . name = "盖聂" ; personObj [ "sex" ] = "男" ;  

In addition to the definition and modification through "obj.propertyName" and "obj [propertyName]", you can also define and modify object properties through the ES5 native API "obj.defineProperty".

 Object.defineProperty(personObj, "name", { value : "剑圣盖聂" } )  
image.png

Object.defineProperty ()

grammar

Object.defineProperty (obj, prop, descriptor)

Parameter Description

obj: required. The object on which to define or modify properties
prop: required. Object property name
descriptor: required. Properties of the target attribute

return value

The object passed into the function. That is, the first parameter obj.

Focus on the attributes of the third parameter value passed in

The type of the descriptor parameter value is an object, which mainly includes field names such as value, writable, enumberable, and configurable . Simple analysis and understand the role and usage of these fields.

value: the value of the property to be manipulated, the default value is undefined

image.png

writable: Whether the attribute value can be overridden. If it is true, it means it can be overridden. Otherwise it means it can't be overridden. The default value is false.

When the value is false:


image.png

When the value is true:

image.png

enumberable: Whether the property can be enumerated. If true, it can be enumerated. Otherwise, it can't be enumerated. The default is false.

When the value is false:

image.png

When the value is true:

image.png

configurable: Whether the target property can be deleted or whether the properties of the property can be re-modified. When true, it can be deleted or modified. Otherwise, the default is false.

This parameter has two functions
  • Can the target attribute be deleted using delete
  • Whether the target attribute can set properties again

When the value is false:

image.png

When the value is true:

image.png

In addition to setting properties for newly defined properties, you can also set properties for existing properties.

image.png

Accessor description

When using accessors to describe the properties of a property, the descriptor parameter allows use of getter and setter methods to set or get the value of a property of an object. When using getter or setter methods, writable and value attributes are not allowed. Use the get / set attribute to define the corresponding method in the attribute.

 var mockData = { name : "剑圣-卫庄" , title : "渊虹" } ; var personObj = { } ; Object . defineProperty ( personObj , "name" , { get : function ( ) { //在获取该属性值得时候触发该函数 debugger ; return mockData . name } , set : function ( value ) { //当设置该属性值的时候触发该函数,设置的新值通过参数value拿到 debugger ; mockData . name = value ; } } )  
image.png



Author: Xing nice small
Link: http://www.jianshu.com/p/5cad911a3630
Source: The copyright of Jianshu belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
Scan QR code to share on WeChat
confirm

Copyright © 2014-2019 buxie520.com Fuzhou website production