【问题标题】:Grouping / nesting get functions分组/嵌套获取函数
【发布时间】:2019-06-24 12:04:46
【问题描述】:

我正在扩展(对不起,来自其他语言的错误名称,我是 JS 新手)Page 对象,通过添加很多函数,例如 firstLevelFunction1firstLevelFunction2。我的目标是将这些函数分组,这样我就可以像这样通过点访问它们:firstLevelGroup.secondLevelFunction1。我以与firstLevelFunction 相同的方式创建firstLevelGroup。函数testLevels 只是为了验证不同级别的行为,当我调用它时,输出是:

一级函数1
一级函数2
{ 获取:[函数:获取] }
{ 获取:[函数:获取] }

虽然我期待:

一级函数1
一级函数2
二级函数1
二级函数2

我的代码:

let Page = require('./../page')
let JsTestingPage = Object.create(Page, {

    firstLevelFunction1: {get: function () { return 'first level function1' }},
    firstLevelFunction2: {get: function () { return 'first level function2' }},

    firstLevelGroup: { get: function () {
        return {
            secondLevelFunction1: {
                get: function () {
                    return 'second level function1'
                }
            },
            secondLevelFunction2: {
                get: function () {
                    return 'second level function2'
                }
            }
        }
    }
    },
    testLevels: {value: function () {
        console.log(this.firstLevelFunction1)
        console.log(this.firstLevelFunction2)
        console.log(this.firstLevelGroup.secondLevelFunction1)
        console.log(this.firstLevelGroup.secondLevelFunction2)
    }}

})
module.exports = JsTestingPage

我也尝试了其他版本,但没有成功。上面的至少不会返回错误。

请告诉我如何对函数进行分组。另外,请随意说,对函数进行分组是没有意义的 :)
顺便说一句,这种结构(第一级)或多或少来自 webdriver.io 框架。将功能分组到第二级是我的想法,以使文件更加清晰和结构化。

【问题讨论】:

  • 感谢您快速准确的回答。它按预期工作。然后我必须决定它是否真的更清楚。如果您愿意,请将其添加为答案,以便我投票并接受它。
  • 关于分号-我来自Java,对我来说分号也更清楚。但这是我公司的政策,我必须适应(eslint 下划线分号)
  • 呃......这是一个糟糕的策略,省略分号会引入难以调试的错误。
  • 我会尝试改变它,当我更多地学习 JS 时。现在我是新手,没有任何影响¯_(ツ)_/¯谢谢你的帮助和建议。

标签: javascript webdriver-io


【解决方案1】:

这是因为你返回了一个对象初始化器,其中get 变成了一个普通的方法名,它不会为内部对象创建一个getter。要解决此问题,请将返回的对象包装在 Object.create(null, {...}) 中(或使用更有意义的原型,如果提供的话),您将得到您所期望的。

let JsTestingPage = Object.create(null, {
  firstLevelFunction1: {
    get: function() {
      return 'first level function1';
    }
  },
  firstLevelFunction2: {
    get: function() {
      return 'first level function2';
    }
  },
  firstLevelGroup: {
    get: function() {
      return Object.create(null, {
        secondLevelFunction1: {
          get: function() {
            return 'second level function1';
          }
        },
        secondLevelFunction2: {
          get: function() {
            return 'second level function2';
          }
        }
      });
    }
  },
  testLevels: {
    value: function() {
      console.log(this.firstLevelFunction1);
      console.log(this.firstLevelFunction2);
      console.log(this.firstLevelGroup.secondLevelFunction1);
      console.log(this.firstLevelGroup.secondLevelFunction2);
    }
  }
});
JsTestingPage.testLevels();

或者在对象初始化器中创建getter:

firstLevelGroup: {
    get: function() {
        return {
            get secondLevelFunction1 () {
                return 'second level function1';
            },
            get secondLevelFunction2 () {
                return 'second level function2';
            }
        }   
    }
},

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多