gpt4 book ai didi

javascript - 如何在函数签名中使用 'this' 关键字来设置默认参数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:32 24 4
gpt4 key购买 nike

我有一个绑定(bind)到 React 组件的函数:

    functionName(param1, param2 = this.state.field) {

}

我从未听说过在 Javascript 的函数签名中使用“this”关键字,我想知道“this”的上下文是包含对象还是全局范围。在我可以在签名中正确使用“this”之前,此函数是否需要绑定(bind)到正确的上下文?

最佳答案

I am wondering if the context of 'this' will be the containing object or the global scope.

都没有。默认值不会在函数外计算,而是 inside the call . this 关键字将指向与方法主体中相同的值 - 调用的接收者。因此,如果该函数绑定(bind)到您的 React 组件,那么 this 就是这样。

关于javascript - 如何在函数签名中使用 'this' 关键字来设置默认参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45040500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com