javascript - 用 Const 设计 JS
问题描述
我有以下代码。有没有更好的方法可以一次使用滚动选项并将其用于所有其他功能。我不确定如何将其重新设计为 const。
module.exports.xyz = function (browser, done) {
browser
.execute('window.scroll(0,2000)')
.clickWaitForElementByCssWithCatch(css, isDisplayed, 30000, 1000)
.nodeify(done);
};
module.exports.abc = function (browser, done) {
browser
.execute('window.scroll(0,2000)')
.clickWaitForElementByCssWithCatch(css, isDisplayed, 30000, 1000)
.nodeify(done);
};
module.exports.efg = function (browser, done) {
browser
.execute('window.scroll(0,2000)')
.clickWaitForElementByCssWithCatch(css, isDisplayed, 30000, 1000)
.nodeify(done);
};
解决方案
字符串 ('window.scroll(0,2000)') 重复 3 次,这就是您从 Sonar 收到的投诉。
您应该在文件顶部定义一个变量:
var js_code = 'window.scroll(0,2000)';
然后将其用作参数:browser.execute(js_code)
推荐阅读
- python - 在python中通过控制台输入数据
- javascript - 使用 jquery 时 Wordpress 菜单消失
- python - Python OpenCV 轮廓排序
- excel - Excel中跨多个工作表的相同单元格的列表以及工作表名称
- c# - 如何更改现有 Visual Studio 项目的编程语言 - 不转换代码
- r - 在 R 中优化函数
- java - 读取属性文件
- javascript - 如何过滤棱镜中的布尔值
- ios - 如何将默认 iOS 元素解析为特定特征?
- django - 如何使用 Djangorestframework 中的 UpdateAPIView 更新模型中的单个字段?