Home >Web Front-end >JS Tutorial >How to develop a custom library
This time I will show you how to develop a custom library and what are the precautions for developing a custom library. The following is a practical case, let's take a look.
Of course, this library is still in its infancy, and the function implementation is relatively simple. It is published here. Everyone is welcome to use this as a basis to improve it. Project address: Hoz.js, everyone is welcome to start, fork, and raise issues.
Declarative template syntax
Learn from redux, flux, etc., and introduce state management
Introducing virtual dom, diff algorithm to improve performance
<p id = "app"> <p> <img src="{{moveImage}}" /> <p>{{moveName}}</p> </p> </p>
var hoz = new Hoz('app', state, changeStore) var state = { moveName: '', moveImage: '' } function changeStore (state, action) { switch (action.type) { case 'SET_NAME': { state.moveName = action.data break } case 'SET_IMAGE': { state.moveImage = action.data break } } } hoz.store.dispatch({ type: 'SET_NAME', data: '后来的我们' })
This is a hoz application, through a concise template Syntax declaratively renders data into the DOM system.
And everything is responsive.
Draw on the ideas of redux in terms of state management, and realize the management of one-way data flow.
Mainly defines the four concepts of state, action, changeStore, and dispatch.
Store data
var state = { moveName: '', moveImage: '' }
Equivalent to the reducer in redux, which stores all operations on data
function changeStore (state, action) { switch (action.type) { case 'SET_NAME': { state.moveName = action.data break } case 'SET_IMAGE': { state.moveImage = action.data break } } }
Receive the action, execute the corresponding method, and modify the data in the state. Different from redux, redux puts back a new state, and it directly operates the current state, because the data in the state has been tracked through the Object.defineProperty method, which will be discussed later.
When we want to modify the data, we must modify the state in the changeStore by submitting the action
hoz.store.dispatch({ type: 'SET_NAME', data: '后来的我们' })
This is hoz State management strategy
view -> dispatch -> action -> changeStore -> state -> view
As applications become increasingly complex and the amount of data increases, it is very difficult to manage the changing status without corresponding data management. It is difficult to observe when and for what reason the state changes.
This means that all data in the application follows the same lifecycle, making the application more predictable and easier to understand.
We can see all the changes that can occur in the state from the changeStore
The only way to modify the state is to submit an action to the changeStore, so Every change in the data will flow through one place, which facilitates our debugging and other operations.
As we all know, due to the large size of dom elements and dom operationsit is easy to cause page rearrangement , the performance of directly operating dom is very, very poor.
So hoz introduced the virtual dom algorithm and used native JavaScript objects to map dom objects, because the operation of native JavaScript objects is faster and simpler.
How to map? For example,
class VNode { constructor (sel, tagName, id, className, el, children, data, text, key) { this.tagName = tagName // p this.sel = sel // p#id.class this.id = id // id this.className = className // [] this.children = children // [] this.el = el // node this.data = data // {} this.key = key this.text = text } } export default VNode
is just a JavaScript object, representing a dom element.
I create a virtual dom tree based on the element pointed to by the id passed in the hoz constructor as the root element. When the data changes, the dom is not directly manipulated, but virtual Perform operations and modifications on the dom tree. Then the diff algorithm is used to compare the old and new virtual DOM trees, and the real DOM is modified in the smallest unit.
How does hoz achieve data responsiveness? A publish/subscribe mode is mainly implemented here by using the Object.defineProperty method. The getter and setter properties of the data in the state are modified through Object.defineProperty. When rendering for the first time, the corresponding subscribers are added to a topic object in the getter. Go, when the data changes, call the notify method of the topic object corresponding to the data in the setter to publish a message to notify each subscriber of the update.
state -> data -> publisher 一对多的关系 | Dep | view -> {{data}} -> subscribers
I hope everyone can use this as a basis to improve it together. Project address: Hoz.js, everyone is welcome to start, fork, and raise issues.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Front-end, HTT, computer and network
Detailed explanation of webkit-font-smoothing font anti-aliasing rendering use cases
The above is the detailed content of How to develop a custom library. For more information, please follow other related articles on the PHP Chinese website!