找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 小程序开发 查看内容

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

作者:模板之家 2018-4-11 10:43 5411人关注

有意见和建议欢迎提在下面! 封装的常用方法, 方法名 参数 介绍 judgeNull value 判断空值,包括{}和,空为true,否则为false judgeString value 判断是否为字符串类型,是为true,否则为fal

有意见和建议欢迎提在下面!

封装的常用方法,
方法名 参数 介绍
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

 

				
  1. const colors = ['1', '2', '3', '4', '5', '6', '7', '1,2', '1,4', '1,5,6,7']
  2. let app = getApp()
  3. let utils = app.utils
  4.  
  5. Page({
  6. data: {
  7. string: ' ',
  8. number: 0,
  9. 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. })


路过

雷人

握手

鲜花

鸡蛋
原作者: 模板之家 来自: 网络收集

全部回复(0)