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

边栏推荐
- Correlation
- Yiwen teaches you how to choose your own NFT trading market
- Plug - in Oil Monkey
- The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
- Compréhension de la technologie gslb (Global Server load balance)
- Yyds dry inventory Chapter 4 of getting started with MySQL: data types that can be stored in the data table
- How does sentinel, a traffic management artifact, make it easy for business parties to access?
- 6.0 kernel driver character driver
- 使用dnSpy對無源碼EXE或DLL進行反編譯並且修改
- UC Berkeley proposes a multitask framework slip
猜你喜欢
![[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)](/img/6c/2d48d441fee1981a271319fd9f6c23.jpg)
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)

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

MySQL——idea连接MySQL
![[actual combat record] record the whole process of the server being attacked (redis vulnerability)](/img/9c/34b916aca2f9270ec4cf4651f0de7e.jpg)
[actual combat record] record the whole process of the server being attacked (redis vulnerability)

Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)

Blue Bridge Cup Guoxin Changtian MCU -- program download (III)

Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)

Day 9 HomeWrok-ClassHierarchyAnalysis

Nacos common configuration

Control loop of program (while loop)
随机推荐
Supply and demand situation and market scale calculation report of China's portable energy storage power PES industry Ⓛ 2022 ~ 2028
Introduction to kubernetes
4. Data splitting of Flink real-time project
How PHP gets all method names of objects
(POJ - 2912) rochambau (weighted concurrent search + enumeration)
gslb(global server load balance)技術的一點理解
[actual combat record] record the whole process of the server being attacked (redis vulnerability)
The White House held an open source security summit, attended by many technology giants
TiDB 之 TiCDC6.0 初体验
Great gods, I want to send two broadcast streams: 1. Load basic data from MySQL and 2. Load changes in basic data from Kafka
No matter how hot the metauniverse is, it cannot be separated from data
Go language slice interview real question 7 consecutive questions
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
Base ring tree Cartesian tree
Intimacy communication -- [repair relationship] - use communication to heal injuries
Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
Rest reference
Imitation Netease cloud music applet
Luogu deep foundation part 1 Introduction to language Chapter 7 functions and structures
Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files