当前位置:网站首页>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);

边栏推荐
- Are the top ten securities companies safe to open accounts and register? Is there any risk?
- How PHP drives mongodb
- The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
- Report on the current situation and development trend of ethoxylated sodium alkyl sulfate industry in the world and China Ⓞ 2022 ~ 2027
- Leetcode problem solving - 230 The k-th smallest element in the binary search tree
- Development trend and market demand analysis report of China's energy storage battery industry Ⓩ 2022 ~ 2028
- Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
- China's TPMS industry demand forecast and future development trend analysis report Ⓐ 2022 ~ 2028
- Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
- js demo 計算本年度還剩下多少天
猜你喜欢

常用sql集合

MySQL——idea连接MySQL

The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation

How PHP adds two numbers

What indicators should be paid attention to in current limit monitoring?

Nacos common configuration
Implementation principle of inheritance, encapsulation and polymorphism

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

仿网易云音乐小程序

Morning flowers and evening flowers
随机推荐
Station B, dark horse programmer, employee management system, access conflict related (there is an unhandled exception at 0x00007ff633a4c54d (in employee management system.Exe): 0xc0000005: read locat
Report on the current situation and development trend of ethoxylated sodium alkyl sulfate industry in the world and China Ⓞ 2022 ~ 2027
Kali2021.4a build PWN environment
C程序设计的初步认识
Memory analyzer (MAT)
Supply and demand situation and market scale calculation report of China's portable energy storage power PES industry Ⓛ 2022 ~ 2028
2022 electrician (elementary) examination questions and electrician (elementary) registration examination
Is it safe and reliable to open an account and register for stock speculation? Is there any risk?
What is the difference between res.send() and res.end() in the node express framework
Code in keil5 -- use the code formatting tool astyle (plug-in)
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
China's coal industry investment strategic planning future production and marketing demand forecast report Ⓘ 2022 ~ 2028
Global and Chinese market of wall mounted kiosks 2022-2028: Research Report on technology, participants, trends, market size and share
鹏城杯 WEB_WP
Rest参考
China's Call Center Industry 14th five year plan direction and operation analysis report Ⓔ 2022 ~ 2028
Global and Chinese market of gallic acid 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)
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
Collection | pytoch common loss function disassembly