【问题标题】:Assign an object to a let keyword when outside of scope超出范围时将对象分配给 let 关键字
【发布时间】:2017-03-24 14:36:54
【问题描述】:

目前,我正在使用一个var,它从另一个hook 分配了一个对象。我想将其更改为 let,但我总是遇到错误。

当前设置:

var tc;

module.exports = {

  before(browser) {
    tc = new func()(x, y); // needs to be assigned here.
  },

  'Test': function (browser) {
    tc // needs to be referenced here too.
  },

};

如何将其更改为 let 并在其他钩子和测试用例中分配/引用它(我正在使用 Mocha)。

【问题讨论】:

    标签: javascript scope mocha.js let


    【解决方案1】:

    使用this.parameter

    module.exports = {
    
      before(browser) {
        this.tc = new func()(x, y); // Now it's assigned to the object context.
      },
    
      'Test': function (browser) {
        return this.tc; // You can reference to the variable inside the object
      },
    
    };
    

    【讨论】:

    • 感谢您的回复。如何在test 函数内引用tc 对象内的参数?
    • 你这样说吗? test: function() { return this.tc.param; }
    • 那种。在“测试”中,我想将一个参数从tc 对象传递给另一个函数。像这样:browser.page.function(tc.parameter, tc.anotherParameter);
    • browser.page.function(this.tc.parameter, this.tc.anotherParameter);,像这样吗?
    猜你喜欢
    • 2013-09-25
    • 2018-09-06
    • 2021-06-16
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    相关资源
    最近更新 更多