再写Javascript闭包
2021/12/17 22:49:59
本文主要是介绍再写Javascript闭包,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Javascript 闭包, 老生常谈。
近期遇到问题时,在可编辑表格中键盘快速的上下移动,每次移动会触发change事件,而change事件中执行的是异步访问后台后填充行(且会有多次访问的情况发生,使用Promise数组同步执行)。
此时的现象为,表格中行数据不完整,有的有的没有。
原来的代码如下:
const rowChangeTasks = [] // 原来的代码,单行逐行填充 arrayValue.forEach((value, idx) => { const changeSqls = changesql.split(';') changeSqls.forEach(cSQL => { const p = () => { return new Promise((resolve, reject) => { const _this = this this.selectedRow = table.tableFullData[theRowIndex + idx] this.selectedRow[columnName] = value const sql = this.replaceAllParams(cSQL) this.ExecCmd({ sql: sql, callName: '表格单元格change事件,执行存储过程', callMethod: 'tableCellDataChangeHandler' }).then(res => { return this.getErrorData(res, 'subtable') }).then(res => { if (res && res.length) { const rowData = Object.assign({}, res[0]) if (Object.prototype.hasOwnProperty.call(rowData, _this.keyname)) { delete rowData[_this.keyname] } // rowData[this.keyname] = `-${idx + theRowIndex}` const currentColumnObject = {} currentColumnObject[columnName] = value Object.assign(table.tableFullData[theRowIndex + idx], currentColumnObject, rowData) return table.cellDefaultValue(table.tableFullData[theRowIndex + idx], theRowIndex + idx) } else { return Promise.resolve() } }).then(() => { table.calcFormulasHandler(table.tableFullData[theRowIndex + idx], columnName) // 计算公式 resolve() }).catch(error => { reject(error) }) }) } rowChangeTasks.push(p) }) if (idx) { table.tableFullData[theRowIndex + idx][columnName] = value } }) this.common.doPromiseArray(rowChangeTasks).then(() => { console.log('结束') })
且后,我想到了用闭包的方式来处理,新的代码如下:
const rowChangeTasks = [] // 原来的代码,单行逐行填充 arrayValue.forEach((value, idx) => { const changeSqls = changesql.split(';') changeSqls.forEach(cSQL => { const p = ((currentRow, currentRowIndex) => { return new Promise((resolve, reject) => { const _this = this // this.selectedRow = table.tableFullData[theRowIndex + idx] currentRow[columnName] = value const sql = this.replaceAllParams(cSQL, { selectedRow: currentRow }) this.ExecCmd({ sql: sql, callName: '表格单元格change事件,执行存储过程', callMethod: 'tableCellDataChangeHandler' }).then(res => { return this.getErrorData(res, 'subtable') }).then(res => { if (res && res.length) { const rowData = Object.assign({}, res[0]) if (Object.prototype.hasOwnProperty.call(rowData, _this.keyname)) { delete rowData[_this.keyname] } // rowData[this.keyname] = `-${idx + theRowIndex}` const currentColumnObject = {} currentColumnObject[columnName] = value Object.assign(currentRow, currentColumnObject, rowData) return table.cellDefaultValue(currentRow, currentRowIndex) } else { return Promise.resolve() } }).then(() => { table.calcFormulasHandler(currentRow, columnName) // 计算公式 resolve() }).catch(error => { reject(error) }) }) })(table.tableFullData[theRowIndex + idx], theRowIndex + idx) rowChangeTasks.push(p) }) if (idx) { table.tableFullData[theRowIndex + idx][columnName] = value } }) this.common.doPromiseArray(rowChangeTasks).then(() => { console.log('结束') })
用了闭包,有效解决了这个问题。
这篇关于再写Javascript闭包的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求