In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.