当前位置:网站首页>This time, thoroughly understand bidirectional data binding 01
This time, thoroughly understand bidirectional data binding 01
2022-07-03 22:04:00 【Always--Learning】
Pre knowledge required for bidirectional data binding
1. Array of reduce Method
Array of reduce The usage scenario of method is that the initial value of this operation depends on the return value of the last operation , For example, the accumulation of values , Get the value of the object attribute in a chain .
reduce Function takes two parameters :
- function ( Previous cumulative value , The value of the currently traversed element )
- Initial value
The sum of the previous cumulative value and the current traversal value should be returned in the function body
const arr = [1,2,3,4];
let res = arr.reduce((pre,cur) => {
return pre + cur;
},0)
console.log(res); // 10
adopt reduce Get the value of the object attribute in a chain
const obj = {
name: ' Zhang San ',
info: {
address: {
location: ' Huairou, Beijing '
}
}
}
const arr = ['info','address','location'];
const location = arr.reduce((pre,cur) => pre[cur],obj);
console.log(location); // Huairou, Beijing
2. Publish subscribe mode
Publish subscribe mode mainly includes the following two classes :
- Dep class
- Responsible for dependency collection
- I have an array , Used to store all subscription information
- There is a way to add subscriptions to the array
- Provide a loop , Cycle through each subscription in the array
- Watcher class
- Responsible for subscribing to events
Here are the most basic Dep Classes and Watcher class :
// Dep Class is mainly used to collect subscriptions
class Dep {
constructor() {
// subs Array , It is used to store the information of all subscribers
this.subs = []
}
// towards subs Array , Add subscriber information
addSub(watcher) {
this.subs.push(watcher);
}
// Method of issuing notice
notify(){
this.subs.forEach(watcher => {
watcher.update();
})
}
}
// Subscriber's class
class Watcher {
constructor(cb) {
this.cb = cb;
}
// Method of triggering callback
update() {
this.cb();
}
}
const w1 = new Watcher(() => {
console.log(' This is the first subscriber ');
})
const w2 = new Watcher(() => {
console.log(' This is the second subscriber ');
})
const dep = new Dep();
dep.addSub(w1);
dep.addSub(w2);
dep.notify()
Subscribers can be understood as DOM Elements , When the data changes ,Vue Every subscriber will be notified , Then the subscriber updates according to the latest data DOM.
3. Use Object.defineProperty() Data hijacking
- adopt get() Hijack value operation .
- adopt set() Hijack assignment operation .
const obj = {
name: ' Zhang San ',
age: 20,
info: {
a: 1,
b: 2
}
}
Object.defineProperty(obj,'name',{
enumerable: true,
configurable: true,
get() {
console.log(' Someone got obj.name Value ');
return " What you get is Zhang San here "
},
set(newVal) {
console.log(' Print out the new value ');
}
})
console.log(obj.name);
obj.name = '555';
console.log(obj);

边栏推荐
- Electronic tube: Literature Research on basic characteristics of 6j1
- 抓包整理外篇——————autoResponder、composer 、statistics [ 三]
- Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
- Global and Chinese market of recycled yarn 2022-2028: Research Report on technology, participants, trends, market size and share
- China's coal industry investment strategic planning future production and marketing demand forecast report Ⓘ 2022 ~ 2028
- Luogu deep foundation part 1 Introduction to language Chapter 7 functions and structures
- How PHP gets all method names of objects
- Uboot migration
- (POJ - 2912) rochambau (weighted concurrent search + enumeration)
- MySQL——idea连接MySQL
猜你喜欢

Why use pycharm to run the use case successfully but cannot exit?
![[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]](/img/55/309c9d52e503405b289bcfc4912be9.jpg)
[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]

IPhone development swift foundation 09 assets

6.0 kernel driver character driver

No matter how hot the metauniverse is, it cannot be separated from data

Yiwen teaches you how to choose your own NFT trading market

Correlation

What is the difference between res.send() and res.end() in the node express framework

Compréhension de la technologie gslb (Global Server load balance)

Minio deployment
随机推荐
A little understanding of GSLB (global server load balance) technology
Asynchronous artifact: implementation principle and usage scenario of completable future
Dahua series books
Report on the development strategy of China's engineering bidding agency and suggestions for the 14th five year plan Ⓙ 2022 ~ 2028
[SRS] build a specified version of SRS
Dynamic research and future planning analysis report of China's urban water supply industry Ⓝ 2022 ~ 2028
pivot ROP Emporium
An expression that regularly matches one of two strings
(POJ - 2912) rochambau (weighted concurrent search + enumeration)
China's Call Center Industry 14th five year plan direction and operation analysis report Ⓔ 2022 ~ 2028
Tkinter Huarong Road 4x4 tutorial III
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- led lamp module (V)
Morning flowers and evening flowers
The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
Let me ask you a question. Have you ever used the asynchronous io of Flink SQL to associate dimension tables in MySQL? I set various settings according to the official website
Leetcode problem solving - 230 The k-th smallest element in the binary search tree
Tidb's initial experience of ticdc6.0
Yiwen teaches you how to choose your own NFT trading market
Memory analyzer (MAT)