JS:传递一个包含函数/方法属性的对象,然后在另一个文件中传递并调用传入的对象函数/方法属性?

我正在努力为登录功能添加额外的检查。如果传递给函数的对象包含属性名称additionalSteps,它将调用additionalSteps 属性。但是,每当我到达调用步骤时,都会收到一条错误消息,指出该属性不是函数或未定义。options.additionalSteps(取决于我如何通过调用对象 property( ) 或将局部变量分配给对象 property( )来调整代码let additionalSteps。)

我想知道是否可以传递一个具有定义函数/方法的属性的对象,然后将该对象传递到导入类(或使用)的函数中,该函数将检查属性名称是否存在,然后调用该对象的require函数如果条件为真。我可以让其他文件定义并处理该函数,但我希望用户按照他们的意愿定义属性函数。

根据我对该主题的研究,我对属性的感觉是不可能通过对象内的另一个文件传递函数/方法。(有关具有函数的对象属性的所有信息和示例似乎暗示这不受支持。)

测试代码:

async function fewMoreSteps({page, options} = {}) {

  console.log('This is the addtional step...')

  await page.waitForSelector('#header_notification_link', {visible: true})

  await page.click('#header_notification_link')

}



describe('Test Login', () => {

  // eslint-disable-next-line jest/no-focused-tests

  it.only('Login with custom function', () => {

    const username = Cypress.env('steamUserName')

    const password = Cypress.env('steamUserNamePW')

    const loginUrl = Cypress.env('loginUrl')

    const loginSelector = Cypress.env('loginSelector')

    const cookieName = Cypress.env('cookieName')

    const socialLoginOptions = {

      username,

      password,

      loginUrl,

      // Add username/pw fields and buttons and addtional steps

      usernameField: '#input_username',

      passwordField: '#input_password',

      passwordSubmitBtn: '#login_btn_signin',

      // make this a global passed function

      additionalSteps: async function({page, options} = {}) {

        /*console.log('This is the addtional step...')

        await page.waitForSelector('#header_notification_link', {visible: true})

        await page.click('#header_notification_link') */ 

        await fewMoreSteps({page, options})

      },

      isPopup: true,

      popupDelay: 6000,

      logs: true,

      headless: false,

      loginSelector: loginSelector,

      postLoginClick: '#account_pulldown',

      postLoginSelector: '#account_dropdown div.popup_menu a.popup_menu_item:first-of-type'

    }


吃鸡游戏
浏览 62回答 1
1回答

慕姐4208626

您可以简单地添加点击事件dropdown-item见下文$('.dropdown-menu .dropdown-item').click(function(){&nbsp; &nbsp; &nbsp;var value = $(this).text();&nbsp; &nbsp; &nbsp;console.log(value)&nbsp; &nbsp; &nbsp;// Update the value and show it to the user&nbsp; &nbsp; &nbsp;$(this).parent().parent().find(".dropdown-toggle").html(value);});<!DOCTYPE html><html><head>&nbsp; <title>Bootstrap 4 Search Box With Dropdown List</title>&nbsp; <meta charset="utf-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1">&nbsp; <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">&nbsp; <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>&nbsp; <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>&nbsp; <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>&nbsp; <body>&nbsp; &nbsp; <div class="search-box col-md-5">&nbsp; &nbsp; &nbsp; <form action="">&nbsp; &nbsp; &nbsp; &nbsp; <div class="input-group mb-3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="input-group-prepend">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-light dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="dropdown-menu">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="dropdown-item" href="#">Category One</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="dropdown-item" href="#">Category Two</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="dropdown-item" href="#">Category Three</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div role="separator" class="dropdown-divider"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="dropdown-item" href="#">Separated Category</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="form-control" aria-label="Search input with dropdown button">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="input-group-append">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-success" type="button">Search</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </div>&nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript