Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use class to optimize program structure in WeChat Mini Programs's actual combat

Shulou Source: shulou.com Published: 2022-06-01 05:16:14 10月04日 Update

This article shows you how to use class optimization program structure in WeChat Mini Programs's actual combat, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Although Javascript is a scripting language, classes can still be defined and used. In this Mini Program, the functions related to the monitoring point are made into a class.

Alarm.js

/ / alarm.js:

Const util = require ('. / util.js')

Const CHECK_BUFFER_SIZE = 3

/ / Constructor

Function Alarm (data) {

This.latitude = data.latitude

This.longitude = data.longitude

This.state = data.state

This.checkBuffer = []

This.title = data.title

This.monitor_type = data.monitor_type

This.action_type = data.action_type

This.meaia_url = data.media_url

This.timer = data.timer

}

/ / define the prototype

Alarm.prototype = {

Constructor:Alarm

SetTitle: function (t) {

This.title = t

}

SetMonitorType:function (m_type) {

This.monitor_type = m_type

}

SetActionType: function (a_type) {

This.action_type = a_type

}

SetMedia: function (url) {

This.media_url = url

}

SetTimer: function (t_name) {

This.timer = t_name

}

CheckLocation: function (latitude, longitude, accuracy) {

Const app = getApp ()

Var that = this

Var distance = util.getDistance (this.latitude, this.longitude, latitude, longitude)

App.addLog (distance + "," + accuracy)

If (distance

< accuracy) { this.checkBuffer.push(1) } else { this.checkBuffer.push(-1) } if (this.checkBuffer.length >

CHECK_BUFFER_SIZE) {

This.checkBuffer.shift ()

}

Var sum = 0

That.checkBuffer.forEach (function (value) {sum + = value})

If (this.moitor_type = = 'close to the monitoring point') {

If (this.state = = 'new') {

If (sum = =-CHECK_BUFFER_SIZE) {

This.state = 'armed'

}

} else if (this.state = = 'armed') {

If (sum = = CHECK_BUFFER_SIZE) {

This.state = 'fired'

}

}

} else {

If (this.state = = 'new') {

If (sum = = CHECK_BUFFER_SIZE) {

This.state = 'armed'

}

} else if (this.state = = 'armed') {

If (sum = =-CHECK_BUFFER_SIZE) {

This.state = 'fired'

}

}

}

}

ExcueteAction: function () {

Play.playVoice (this.media_url)

}

}

Module.exports = {

Alarm:Alarm

}

Code description

In alarm.js, a class named Alarm is defined.

First, an Alarm constructor is defined, which takes an object data as a parameter. The function of the function is to take the data from the data and set it to the corresponding data member.

Then a prototype object is defined, and several functions are defined inside it. All objects obtained through new Alarm are based on what is defined in protype. In other words, you can all use the functions defined in prototype.

The last module.export statement defines the exposed function of this module.

The contents of the function will be explained in subsequent articles.

The use of classes

Import CLA

First, you need to import the Alarm class in the user module. Corresponding to module.export in Alarm.js, you can use the following ways:

Import {Alarm} from'. / utils/alarm.js'

Create objects, work with object

Var alarm = new Alarm ({latitude:38,longitude:120})

Alarm.setActionType ("play cue tone")

Code download link

Alarm.js

Https://raw.githubusercontent.com/xueweiguo/alarmmap/master/utils/alarm.js

The above content is how to use class optimization program structure in WeChat Mini Programs's actual combat. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Function program object content function actual combat structure code prototype that is skills data articles modules monitoring points knowledge monitoring conciseness and then to the point. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Xiaomi Linux Shulou Technology