当前位置:网站首页>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);
边栏推荐
- IPhone development swift foundation 09 assets
- js demo 計算本年度還剩下多少天
- (POJ - 2912) rochambau (weighted concurrent search + enumeration)
- DR-AP40X9-A-Qualcomm-IPQ-4019-IPQ-4029-5G-4G-LTE-aluminum-body-dual-band-wifi-router-2.4GHZ-5GHz-QSD
- Dahua series books
- MySQL——JDBC
- [vulnhub shooting range] impulse: lupinone
- JS demo calculate how many days are left in this year
- Leetcode problem solving - 235 Nearest common ancestor of binary search tree
- [dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
猜你喜欢
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
6.0 kernel driver character driver
Nacos common configuration
What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
Mysql database - Advanced SQL statement (I)
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
MySQL——JDBC
The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
MySQL——idea连接MySQL
随机推荐
Farmersworld farmers world, no faith, how to talk about success?
90 后,辞职创业,说要卷死云数据库
Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
[vulnhub shooting range] impulse: lupinone
treevalue——Master Nested Data Like Tensor
DR-AP40X9-A-Qualcomm-IPQ-4019-IPQ-4029-5G-4G-LTE-aluminum-body-dual-band-wifi-router-2.4GHZ-5GHz-QSD
Global and Chinese market of AC induction motors 2022-2028: Research Report on technology, participants, trends, market size and share
The latest analysis of crane driver (limited to bridge crane) in 2022 and the test questions and analysis of crane driver (limited to bridge crane)
Market layout planning and latest dynamic analysis report of China's smart public security industry Ⓕ 2022 ~ 2028
股票炒股开户注册安全靠谱吗?有没有风险的?
DR-NAS26-Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-5GHz-high-power-Mini-PCIe-Wi-Fi-Module
MySQL——idea连接MySQL
China's coal industry investment strategic planning future production and marketing demand forecast report Ⓘ 2022 ~ 2028
UC Berkeley proposes a multitask framework slip
Oil monkey plug-in
[actual combat record] record the whole process of the server being attacked (redis vulnerability)
How PHP drives mongodb
Base ring tree Cartesian tree
Leetcode problem solving - 230 The k-th smallest element in the binary search tree
Remember the experience of automatically jumping to spinach station when the home page was tampered with