How to implement simple data monitoring with JS

How to implement simple data monitoring with JS

Overview

Mainly use Object.defineProperty to implement data binding similar to vue.

first step

const data = {
  name: "tom",
  age: 14
}
Object.defineProperty(data, "name", {
  get(){
    return "name was read"
  },
 set(val){
   console.log('I was assigned',val)
 }
})
//Put this code in the browser console to view the effect console.log(data.name)

The output data.name is not tom, but name is read, because defineProperty monitors and hijacks the name field of data and modifies the value that the name field should have returned.

Step 2

const _data = { ...data }
for(let i in data){
  Object.defineProperty(data, i, {
    get(){
      return _data[i]+"modified by js"
    },
    set(val){
      _data[i] = val;
    }
  })
}

Why is a separate _data needed?

Answer: The data field is monitored and the return attribute of the field is modified. The resulting impact is that each time the monitored field in data is obtained, the browser will call the value returned by get. If you directly return return data[i] in get, the browser will continuously call the get method, thus entering an infinite loop.

Add a little more data to data

const data = {
  name: "tom",
  age: 14,
  friend:
        "name1": "Zhang San",
        "name2": "Li Si",
        "name3": "Wang Wu",
        "name4": "Zhao Liu"
  },
}

Formatting initial value

const createNewWatch = (val, path, parentKey, event) => {
       //If the value is not of object type, return the value directly if(typeof val != 'object') return val;
       //On the contrary, if it is of object type, then call WatchObject to traverse and monitor the child elements //WatchObject will be created in the following code return WatchObject(val,path.concat(parentKey), event)
    }
Guangzhou brand design company https://www.houdianzi.com

Format object and monitor value

const WatchObject = (data, path, event) => {
  function WatchObject(){
    for(var key in data){
        //Call the function created previously to format val
        data[key] = createNewWatch(data[key], path, key, event)
        //Create a listener for the data key defineProperty(this, key, data[key], path.concat(key), event)
    }
  }
  return new WatchObject()
}

Finally, execute the code and a simple data monitoring is completed.

const b = WatchObject(data,[],{ 
    set(path,val){ 
      console.log(path,val) 
    } 
})

The above is the details of how to use JS to implement simple data monitoring. For more information about JS data monitoring, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to monitor div resize event in JS
  • JavaScript monitoring combination key ideas and code implementation
  • How to monitor keyboard input and mouse clicks in JavaScript
  • NodeJS multiple ways to create WebSocket listeners (three)
  • Detailed explanation of sample code for monitoring URL changes without refreshing in js
  • JavaScript monitors keyboard events code implementation
  • Use JS to listen for keydown events
  • js implements watch code to monitor data changes
  • Detailed explanation of javascript event monitoring and event delegation examples

<<:  MySQL view principles and basic operation examples

>>:  How to install Oracle on Windows Server 2016

Recommend

Parsing Apache Avro Data in One Article

Abstract: This article will demonstrate how to se...

Linux CentOS6.5 yum install mysql5.6

This article shares the simple process of install...

Use of SerialPort module in Node.js

Table of contents Purpose Module Installation Bas...

Methods to enhance access control security in Linux kernel

background Some time ago, our project team was he...

How to reset Zabbix password (one-step)

Problem Description Since we don't log in to ...

js realizes packaging multiple pictures into zip

Table of contents 1. Import files 2. HTML page 3....

Introduction to ufw firewall in Linux

Let's take a look at ufw (Uncomplicated Firew...

Summary of the advantages of Vue3 vs. Vue2

Table of contents 1. Why do we need vue3? 2. Adva...

Docker meets Intellij IDEA, Java development improves productivity tenfold

Table of contents 1. Preparation before developme...

MySql 8.0.11 installation and configuration tutorial

Official website address: https://dev.mysql.com/d...

How to deploy Vue project under nginx

Today I will use the server nginx, and I also nee...

Sample code using scss in uni-app

Pitfalls encountered I spent the whole afternoon ...

How to install MySQL 8.0 database on M1 chip (picture and text)

1. Download First of all, I would like to recomme...