自动化 |网络驱动 |在元素名称之前使用get有什么用?

Automation | Webdriverio | What is the use of using get before element name?

在我的框架中,我有一个页面 xyz,其元素属性在 xyz.page.js

中定义
    get logInPromptWindow() {
      return browser.element("//div[contains(@class,'abcdef')]");
    }

    logInPromptWindow() {
      return browser.element("//div[contains(@class,'abcdef')]");
    }
  1. 使用“get”有什么用吗?
  2. 这两个元素会被视为不同的元素吗?

get 是 javascript 中的 getter 关键字。它允许您 bing 一个变量到 属性.

与getter:

    const obj = {
      log: ['a', 'b', 'c'],
      get latest() {
        if (this.log.length === 0) {
          return undefined;
        }
        return this.log[this.log.length - 1];
      }
    };
    
    console.log(obj.latest);
    // expected output: "c"
 
    console.log(obj);
    //prints the final object state

没有getter:

    const obj = {
      log: ['a', 'b', 'c'],
      latest() {
        if (this.log.length === 0) {
          return undefined;
        }
        return this.log[this.log.length - 1];
      }
    };

    console.log(obj.latest());
    // expected output: "c"

    console.log(obj);
    //prints the final object state

在getter中,函数被解析为具有指定值的属性。虽然没有 getter 它将被解析为一个 属性 函数作为你必须调用函数触发它的值

或者简单来说,它充当一个自调用函数