【造轮子】utils工具类(常用方法的封装)

20170119(微信小程序utils).rar


封装的常用方法,

方法名 参数 介绍
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

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. booleantrue,
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. })


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值