Detailed explanation of JS data access object pattern
This article mainly brings you an example explanation of the data access object pattern of JS design pattern. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.
Question
Most websites will store some data (such as user token) on the front end to transfer values between pages. For some large-scale web applications, It may store a lot of data, data management will become complicated, and a large project is jointly developed by multiple programmers. At this time, you will encounter a problem: how to ensure that your own data will not overwrite other data. What about humans? Because everyone uses the same WebStorage object on a page, you can't record the keys used by everyone. At this time, you can use the data access object pattern to solve the problem.
Introduction
HTML5 provides two new methods for storing data on the client: localStorage and sessionStorage. They are two storage mechanisms provided by the Web Storage API. The difference is that the former is permanent storage. , and the latter is data transfer limited to the current window, and the data stored in it will be deleted when the current session ends. The specific contents of localStorage and sessionStorage will not be introduced here. We will mainly discuss how to use them reasonably in actual development.
Data Access Object Pattern (DAO)
The data access object pattern encapsulates the access and storage of data sources and provides a data access object class responsible for managing and operating the stored data. Standardizes the data storage format, similar to the DAO layer in the background.
Since WebStorage uses Key-Value to access data and can only store strings (any type will be converted to a string when stored, and type conversion is required when reading), so we can Standardize the format of Key, such as module name + Key, developer + Key, etc. You can also add a prefix to the value to describe the data, such as adding a timestamp of the data expiration date to manage the life cycle of the data. The specific format project team can define it themselves, mainly to facilitate management and prevent conflicts. After agreeing on the specifications, they can start defining data access objects.
The following takes localStorage as an example to introduce the definition and use of data access object classes.
Code example
Basic structure of DAO class
The basic structure of the data access object class is as follows. We add a prefix to the key value to avoid key value conflicts, and in Add the data expiration timestamp and separator to the value, and then determine whether it has expired when obtaining the value. This allows for more flexible management of the life cycle of stored data. A callback method is also used here to facilitate obtaining the specific results of the data access process and performing related operations when necessary.
/** * LocalStorage数据访问类 * @param {string} prefix Key前缀 * @param {string} timeSplit 时间戳与存储数据之间的分割符 */ var Dao = function (prefix, timeSplit) { this.prefix = prefix; this.timeSplit = timeSplit || '|-|'; } // LocalStorage数据访问类原型方法 Dao.prototype = { // 操作状态 status: { SUCCESS: 0, // 成功 FAILURE: 1, // 失败 OVERFLOW: 2, // 溢出 TIMEOUT: 3 // 过期 }, // 本地存储对象 storage: localStorage || window.localStorage, // 获取带前缀的真实键值 getKey: function (key) { return this.prefix + key; }, // 添加(修改)数据 set: function (key, value, callback, time) { ... }, // 获取数据 get: function (key, callback) { ... }, // 删除数据 remove: function (key, callback) { ... } }
Add (modify) data
/** * 添加(修改)数据 * @param key 数据字段标识 * @param value 数据值 * @param callback 回调函数 * @param time 过期时间 */ set: function (key, value, callback, time) { // 默认为成功状态 var status = this.status.SUCCESS, key = this.getKey(key); try { // 获取过期时间戳 time = new Date(time).getTime() || time.getTime(); } catch (e) { // 未设置过期时间时默认为一个月 time = new Date().getTime() + 1000 * 60 * 60 * 24 * 30; } try { // 向本地存储中添加(修改)数据 this.storage.setItem(key, time + this.timeSplit + value); } catch (e) { // 发生溢出 status = this.status.OVERFLOW; } // 执行回调并传入参数 callback && callback.call(this, status, key, value); }
Get data
/** * 获取数据 * @param key 数据字段标识 * @param callback 回调函数 */ get: function (key, callback) { var key = this.getKey(key), status = this.status.SUCCESS, // 获取数据状态 value = null; // 获取数据值 try { // 从本地存储获取数据 value = this.storage.getItem(key); } catch (e) { // 获取数据失败 status = this.status.FAILURE; value = null; } // 如果成功获取数据 if (status !== this.status.FAILURE) { var index = value.indexOf(this.timeSplit), timeSplitLen = this.timeSplit.length, // 获取时间戳 time = value.slice(0, index); // 判断数据是否未过期 if (new Date(1*time).getTime() > new Date().getTime() || time == 0) { // 获取数据值 value = value.slice(index + timeSplitLen); } else { // 数据已过期,删除数据 value = null; status = this.status.TIMEOUT; this.remove(key); } } // 执行回调 callback && callback.call(this, status, value); // 返回结果值 return value; }
Delete data
/** * 删除数据 * @param key 数据字段标识 * @param callback 回调函数 */ remove: function (key, callback) { // 设置默认状态为失败 var status = this.status.FAILURE, key = this.getKey(key), value = null; try { // 获取数据值 value = this.storage.getItem(key); } catch (e) { // 数据不存在,不采取操作 } // 如果数据存在 if (value) { try { // 删除数据 this.storage.removeItem(key); status = this.status.SUCCESS; } catch (e) { // 数据删除失败,不采取操作 } } // 执行回调并传入参数,删除成功则传入被删除的数据值 callback && callback.call(this, status, status > 0 ? null : value.slice(value.indexOf(this.timeSplit) + this.timeSplit.length)); }
Usage
var dao = new Dao('myModule_'); // 添加/修改数据 dao.set('token', 'abc', function () { console.log(arguments); }); // 获取数据 var value = dao.get('token', function () { console.log(arguments); }); console.log(value); // 删除数据 dao.remove('token', function () { console.log(arguments); });
Write at the end
In fact, the data access object mode is more suitable for server-side database operations, such as operating MongoDB in nodejs. By encapsulating the database addition, deletion, modification and query operations, we can facilitate the management of front-end storage without having to worry about operating the database. DAO has provided us with a convenient and unified interface, so that we don’t have to worry about affecting other people’s data during team development.
Related recommendations:
php design pattern DAO (Data Access Object Pattern)
Yii Learning Summary Data Access Object (DAO), yiidao_PHP tutorial
javascript object-oriented analysis of two ways to access object properties_javascript skills
The above is the detailed content of Detailed explanation of JS data access object pattern. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use