In this article, we learn how computed works by implementing a simple version of the function that has the same function as computed in Vue. Friends who are interested in how computed works in Vue.js should learn together
JS attributes:
JavaScript has a feature that is Object.defineProperty
, it can do many things, but I only focus on one of these methods in this article:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
(Obeject.defineProperty is a method of Object, the first parameter is the name of the object, the second parameter is the name of the property to be set, and the third parameter is an object, which can set whether the property can be modified, writable, etc. This article mainly uses the accessor of Object.defineProperty Attributes, interested friends can google or check Js High and Programming)
Although person.age looks like it accesses an attribute of the object, in fact we are running a function internally.
A basically responsive Vue.js
Vue.js internally builds an object that can convert ordinary objects into values that can be observed (responsive properties). The following shows you a simplified version of how to add response attributes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Interestingly, 25 and 'Brazil' are still variables inside the closure, and val only becomes valid when new values are assigned to them. will change. person.country does not have the value 'Brazil', but the getter function has the value 'Brazil'.
Declaring a computed property
Let’s create a function defineComputed that defines a computed property. This function is the same as when you usually use computed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Let's write a simple defineComputed function that supports calling compute methods, but there is no need for it to support updateCallback at the moment.
1 2 3 4 5 6 7 8 9 10 11 |
|
There are two problems with this function:
The calculation function computeFunc () is executed every time the calculated property is accessed
It does not know when to update (i.e. when we Update an attribute in a data, and the data attribute will also be updated in the calculated attribute)
1 2 3 4 5 |
|
Add a dependency
Let’s add A global variable Dep:
1 2 3 |
|
This is a dependency, and then we use a Sao operation to update the defineComputed function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Now let us return to the response attributes set before:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
We can update the onDependencyUpdated function after the function defined by the calculated property triggers the update callback.
1 2 3 4 5 |
|
Put them together:
Let us revisit our calculated attribute person.status:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The above is what I compiled for everyone, I hope it will be useful in the future Everyone is helpful.
Related articles:
About the output stream at the end of the servlet in Ajax technology
Ajax php realizes three-level linkage of product classification
Ajax technology composition and core principle analysis
The above is the detailed content of How computed works in Vue.js. For more information, please follow other related articles on the PHP Chinese website!