有意见和建议欢迎提在下面!
封装的常用方法,
方法名 |
参数 |
介绍 |
judgeNull |
value |
判断空值,包括{}和[],空为true,否则为false |
judgeString |
value |
判断是否为字符串类型,是为true,否则为false |
judgeNumber |
value |
判断是否为数字类型,是为true,否则为false |
judgeBoolean |
value |
判断是否为布尔类型,是为true,否则为false |
judgeArray |
value |
判断是否为数组类型,是为true,否则为false |
judgeObject |
value |
判断是否为对象类型,是为true,否则为false |
judgeFunction |
value |
判断是否为方法类型,是为true,否则为false |
mergeObject |
ob1,ob2... |
合并对象,深层克隆 |
getApp |
|
同微信官方getApp |
getCurrentPages |
|
同微信官方getCurrentPages |
getCurrentPage |
|
获取当前页 |
getCurrentPath |
|
获取当前页路径 |
getPath |
targetPath |
getRelativePath的简易封装,不需要当前路径,只需要目标路径 |
getRelativePath |
currentPath,targetPath |
获取两个路径之间相对路径 |
getTimestamp |
|
获取时间戳 |
getClassName |
object key对应class,value对应true或false |
获取class的方法,使用方法同ng,比较方便多class生成 |
具体见附件
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
-
}
-
})
|
|