gpt4 book ai didi

javascript - Javascript 中 consice 方法中的词法 this

转载 作者:行者123 更新时间:2023-11-30 08:26:32 25 4
gpt4 key购买 nike

下面是我的代码和解释。我有一个类,其成员函数和变量如下。 functionOne & functionTwo 是简洁的方法。

function MyUtils() {
this.actions = {
functionOne(param1, param2) {
console.log('This is first function which is not in action');
},
functionTwo(param1, param2) {
console.log('This is second function calling function three');
//HOW DO I CALL functionThree HERE?
// I tried doing - this.functionThree() but not working
}
}

this.functionThree() {
console.log('This is third function');
}
}

如果函数二被调用,那么我希望函数三在其中被调用?

最佳答案

你可以不用这个关键字,这在 javascript 中使用了闭包语法:

function MyUtils() {

function functionThree() {
console.log('This is third function');
}

this.actions = {
functionOne(param1, param2) {
console.log('This is first function which is not in action');
},
functionTwo(param1, param2) {
console.log('This is second function calling function three');
//HOW DO I CALL functionThree HERE?
// I tried doing - this.functionThree() but not working
functionThree();

}
}


}

这是 repl 的输出:(找到 here)

clear
Native Browser JavaScript

This is second function calling function three
This is third function
=> undefined

关于javascript - Javascript 中 consice 方法中的词法 this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44673050/

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