ES6 (Decorator (decorator))

Decorator (decorator)

1. Basic Concepts

Functions are used to modify the behavior of a class

1. Decorator is a function 2. The behavior of the class can be modified by the Decorator (the decorator) (the function of the extended class) 3. The Decorator (the decorator) is only useful in the scope of the class

2. Basic usage

see code

 

1. Definition of Decorator

result

Modify the time attribute

result:

(modification is not allowed, to achieve "read-only" function)

 2. Operate outside the class (must be in front of the class)

result:

Third Party Libraries:

Second, the example (selling point system)

1.

2. Instantiate

3. Run

result:

Advantages: 1. The selling point system is extracted into a reusable module, and the reusability is improved

           2. The introduction and maintainability of business code are improved

This is also the important role of Decorator

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325094152&siteId=291194637