封装的常用方法,
具体见附件
app.js
1.
import
utils from
'./utils/index'
;
2.
3.
App({
4.
onLaunch() {
5.
this
.utils =
new
utils()
6.
}
7.
})
index.js
01.
const
colors = [
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'1,2'
,
'1,4'
,
'1,5,6,7'
]
02.
let app = getApp()
03.
let utils = app.utils
04.
05.
Page({
06.
data: {
07.
string:
' '
,
08.
number:
0
,
09.
boolean
:
true
,
10.
object: {},
11.
array: []
12.
},
13.
onLoad() {
14.
console.log(
this
.data.string)
15.
console.log(
'this.data.string is string : '
+ utils.judgeString(
this
.data.string))
16.
console.log(
'this.data.string is null : '
+ utils.judgeNull(
this
.data.string))
17.
console.log(
this
.data.number)
18.
console.log(
'this.data.number is number : '
+ utils.judgeNumber(
this
.data.number))
19.
console.log(
this
.data.
boolean
)
20.
console.log(
'this.data.boolean is boolean : '
+ utils.judgeBoolean(
this
.data.
boolean
))
21.
console.log(
this
.data.object)
22.
console.log(
'this.data.object is object : '
+ utils.judgeObject(
this
.data.object))
23.
console.log(
'this.data.object is null : '
+ utils.judgeNull(
this
.data.object))
24.
console.log(
this
.data.array)
25.
console.log(
'this.data.array is array : '
+ utils.judgeArray(
this
.data.array))
26.
console.log(
'this.data.string is null : '
+ utils.judgeNull(
this
.data.array))
27.
let aObject = {
28.
aa: {
29.
a:
1
30.
}
31.
}
32.
let bObject = {
33.
bb: {
34.
b:
2
35.
}
36.
}
37.
let cObject = utils.mergeObject(aObject, bObject)
38.
console.log(aObject)
39.
console.log(bObject)
40.
console.log(cObject)
41.
aObject.aa.a =
2
42.
console.log(cObject)
43.
let currentPath = utils.getCurrentPath()
44.
let targetPath =
'pages/list/list'
45.
let relativePath = utils.getPath(targetPath)
46.
console.log(currentPath)
47.
console.log(targetPath)
48.
console.log(relativePath)
49.
console.log(
this
.getColors(colors))
50.
},
51.
getColors(colors) {
52.
let returnColors = []
53.
for
(var a =
0
; a < colors.length; a++) {
54.
let color =
','
+ colors[a] +
','
55.
let className = utils.getClassName({
56.
'block'
:
true
,
57.
'red'
: color.indexOf(
',1,'
) > -
1
,
58.
'orange'
: color.indexOf(
',2,'
) > -
1
,
59.
'yellow'
: color.indexOf(
',3,'
) > -
1
,
60.
'green'
: color.indexOf(
',4,'
) > -
1
,
61.
'lightblue'
: color.indexOf(
',5,'
) > -
1
,
62.
'blue'
: color.indexOf(
',6,'
) > -
1
,
63.
'purple'
: color.indexOf(
',7,'
) > -
1
64.
})
65.
returnColors.push(className)
66.
}
67.
return
returnColors
68.
}
69.
})