首页 > 其他分享 >Thinking--函数参数Normalize思想在前端中的应用

Thinking--函数参数Normalize思想在前端中的应用

时间:2023-03-08 19:32:56浏览次数:53  
标签:Normalize el success Thinking binding value 函数参数 clipboard ._


Thinking系列,旨在利用10分钟的时间传达一种可落地的编程思想。

Normalize

标准化:Normalize

发组件过程中,为了提高组件的灵活性,我们通常需要支持多种传参格式,如何优雅的控制和组件内部解耦变得尤为重要!

示例: Vue的 clip-board 指令,支持两种传参方式。

  • 如果是字符串,则为要拷贝的文本内容
  • 如果是对象,对象中的text属性,为要拷贝的文本内容
<div v-clip-board="txt"></div>
<div v-clip-board="{text: 'txt', success, error}"></div>

传统写法

const _noop = function () { return {} }
const ClipBoard = require('clipboard')
export default {
name: 'clip-board',
hooks: {
bind: function (el, binding, vnode) {
const clipboard = new ClipBoard(el, {
action () { return binding.arg === 'cut' ? 'cut' : 'copy' }
})
// 判断是否为对象,并解构对象属性
if (typeof (binding.value) === 'object') {
let { success = _noop, error = _noop } = binding.value
clipboard.on('success', e => {
success()
})
clipboard.on('error', e => {
error()
})
}
el._clipboard = clipboard
},
update (el, binding) {
el._clipboard.text = function () {
// 判断是否为对象,处理相关值
return typeof (binding.value) === 'string' ? binding.value : binding.value.value
}
},
unbind (el, binding) {
el._clipboard.destroy()
delete el._clipboard
}
}

如果增加第三种参数支持,如数组格式,该如何处理?​​bind​​​ 和 ​​update​​ 方法中都需要增加判断分支。

Normalize后

const _noop = function () {}
// 集中化处理参数
const normalizeProps = function (param) {
let obj = {
text: '',
action: param.arg ? param.arg : 'copy',
success: _noop,
error: _noop
}
let type = Object.prototype.toString.call(param.value).match(/^\[object (.*)\]$/)[1].toLowerCase()
switch (type) {
case 'string':
obj.text = param.value
break
case 'object':
obj = Object.assign({}, obj, param.value)
break
default:
break
}
return obj
}

const ClipBoard = require('clipboard')
export default {
name: 'clip-board',
hooks: {
bind: function (el, binding, vnode) {
let { action, success, error } = normalizeProps(binding)
const clipboard = new ClipBoard(el, {
action () { return action }
})
clipboard.on('success', e => {
success()
})
clipboard.on('error', e => {
error()
})
el._clipboard = clipboard
},
update (el, binding) {
el._clipboard.text = function () {
return normalizeProps(binding)['text']
}
},
unbind (el, binding) {
el._clipboard.destroy()
delete el._clipboard
}
}
}

优势:

  1. 统一了判断入口,核心代码省掉了不必要的分支判断,简单明了;
  2. 后续需要增加其他格式属性时,核心代码无需修改,只需调整 normalize 函数。

哪里还有类似的场景出现?

以 Vue 中 Props 为例,进行说明
​​​https://github.com/vuejs/vue/blob/dev/src/core/util/options.js#L298​

props: ["propA"]
props: {
propB: Object,
propC: {
type: String
}
}

Thinking--函数参数Normalize思想在前端中的应用_标准化参数

延伸

外观设计模式:

Thinking--函数参数Normalize思想在前端中的应用_thinking_02


标签:Normalize,el,success,Thinking,binding,value,函数参数,clipboard,._
From: https://blog.51cto.com/u_15998238/6108714

相关文章

  • Thinking--快速找出故障机器(异或)
    Thinking系列,旨在利用10分钟的时间传达一种可落地的编程思想。假设一个机器仅存储一个标号为ID(数值)的记录,且该数据会保存备份(即,两个机器存储了同样的数据;类似于双节点部署)......
  • Thinking--AOP思想在前端中的应用
    Thinking系列,旨在利用10分钟的时间传达一种可落地的编程思想。AOPAOP(AspectOrientedProgramming),面向切面编程。其从主关注点中分离出横切关注点是面向侧面的程序设计的核......
  • Rethinking the Heatmap Regression for Bottom-up Human Pose Estimation
    本文的主要思想就是对heatmap图进行一个权重缩放。weight是作者提出的一个思路让模型将低输出值的位置加大权重,高输出值给予小权重,低输出值给与大全中。scaled_gt就是scale......
  • Rethinking CNN Models for Audio Classification
    WhatenablestheImageNetpretrainedmodelstolearnusefulaudiorepresentations,wesystematicallystudyhowmuchofpretrainedweightsisusefulforlearnin......
  • ES6-ES11 函数参数的默认值设置
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>函数参......
  • 二维数组的声明及其作为函数参数的方式
    ◆概要本文以3行2列的二维数组为例,介绍了如何声明自动存储、静态存储和动态存储的二维数组,及其如何将它们作为函数参数进行传递的方式。◆实现处理自动存储或静态......
  • C++主函数参数
    学习C++主函数的参数输入,用于从commandline中读取参数,下面以读取视频文件为例进行说明#include<iostream>#include<fstream>#include<string>#include<opencv2/op......
  • Python中函数参数是如何传递的?
    Python函数相比对于多数程序员来说不陌生吧,那么Python函数中的参数是如何传递的呢?首先我将会以代码图文的形式给大家展现以下,看看是不是和您所认知的Python函数一样的结果?......
  • JavaScript normalize function All In One
    JavaScriptnormalizefunctionAllInOneUnicodestring/Emojistring国际化String.prototype.normalize()Thenormalize()methodreturnstheUnicodeNormaliz......
  • 一文看懂 Python 中的函数参数
    函数定义中的参数也就是形式参数,规定了在调用函数时如何传递实际参数以及这些参数有无默认值。实参传递方式deff(a):print(a)实参传递方式有两种,位置和关键字。......