Summary of Vue front-end interview questions (36) How to understand the role of Object.defineProperty() method

Function : directly define a new attribute on an object, or modify an existing attribute of an object, and return this object

parameter

Object.defineProperty( obj, prop, desc );

obj The object on which attributes are to be defined.

prop The name of the property to be defined or modified.

descriptor The attribute descriptor to be defined or modified.

return value

Returns the object being operated on, that is, returns the obj parameter

use

Vue implements two-way binding through getter-setter functions

Commonly known as attribute mounter

Object.observe()(es7), which specifically monitors changes in the object array, also uses this method

Two optional keys

get: A method that provides a getter for a property, if there is no getter, it is undefined. When the property is accessed, the method will be executed. When the method is executed, no parameters will be passed in, but the this object will be passed in (due to inheritance, this here is not necessarily the object that defines the property).
The default is undefined.

set: A method that provides a setter to a property, if there is no setter, it is undefined. When the attribute value is modified, the method is triggered. This method will accept the only parameter, which is the new parameter value of the attribute.
The default is undefined.

Guess you like

Origin blog.csdn.net/Rick_and_mode/article/details/108657846