封装的常用方法,
具体见附件
app.js
import utils from './utils/index';
App({
onLaunch() {
this.utils = new utils()
}
})index.js
const colors = ['1', '2', '3', '4', '5', '6', '7', '1,2', '1,4', '1,5,6,7']
let app = getApp()
let utils = app.utils
Page({
data: {
string: ' ',
number: 0,
boolean: true,
object: {},
array: []
},
onLoad() {
console.log(this.data.string)
console.log('this.data.string is string : ' + utils.judgeString(this.data.string))
console.log('this.data.string is null : ' + utils.judgeNull(this.data.string))
console.log(this.data.number)
console.log('this.data.number is number : ' + utils.judgeNumber(this.data.number))
console.log(this.data.boolean)
console.log('this.data.boolean is boolean : ' + utils.judgeBoolean(this.data.boolean))
console.log(this.data.object)
console.log('this.data.object is object : ' + utils.judgeObject(this.data.object))
console.log('this.data.object is null : ' + utils.judgeNull(this.data.object))
console.log(this.data.array)
console.log('this.data.array is array : ' + utils.judgeArray(this.data.array))
console.log('this.data.string is null : ' + utils.judgeNull(this.data.array))
let aObject = {
aa: {
a: 1
}
}
let bObject = {
bb: {
b: 2
}
}
let cObject = utils.mergeObject(aObject, bObject)
console.log(aObject)
console.log(bObject)
console.log(cObject)
aObject.aa.a = 2
console.log(cObject)
let currentPath = utils.getCurrentPath()
let targetPath = 'pages/list/list'
let relativePath = utils.getPath(targetPath)
console.log(currentPath)
console.log(targetPath)
console.log(relativePath)
console.log(this.getColors(colors))
},
getColors(colors) {
let returnColors = []
for (var a = 0; a < colors.length; a++) {
let color = ',' + colors[a] + ','
let className = utils.getClassName({
'block': true,
'red': color.indexOf(',1,') > -1,
'orange': color.indexOf(',2,') > -1,
'yellow': color.indexOf(',3,') > -1,
'green': color.indexOf(',4,') > -1,
'lightblue': color.indexOf(',5,') > -1,
'blue': color.indexOf(',6,') > -1,
'purple': color.indexOf(',7,') > -1
})
returnColors.push(className)
}
return returnColors
}
})作者: