Home > Web Front-end > JS Tutorial > body text

How to develop a custom library

php中世界最好的语言
Release: 2018-05-25 13:38:30
Original
2082 people have browsed it

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.

Features

  • Declarative template syntax

  • Learn from redux, flux, etc., and introduce state management

  • Introducing virtual dom, diff algorithm to improve performance

Declarative syntax

  <p id = "app">
      <p>
          <img src="{{moveImage}}" />
          <p>{{moveName}}</p>
      </p>
  </p>
Copy after login
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: '后来的我们'
})
Copy after login

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, flux, etc., and introduce state management

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.

state

Store data

var state = {
  moveName: '',
  moveImage: ''
}
Copy after login

changeStore

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
    }
  }
}
Copy after login

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.

action and dispatch

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: '后来的我们'
})
Copy after login

This is hoz State management strategy

 view -> dispatch -> action -> changeStore -> state -> view
Copy after login

Benefits

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.

  1. This means that all data in the application follows the same lifecycle, making the application more predictable and easier to understand.

  2. We can see all the changes that can occur in the state from the changeStore

  3. 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.

Introducing virtual dom and diff algorithm

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
Copy after login

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.

Data Responsiveness Principle

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!