[英]how to call a arrow function inside another function from outside?
function x(name){
this.name = name;
const arrow =() =>{
setTimeout(() => {
console.log("Arrow" + this.name)
},100)
}
const func1 = () => {
setTimeout(() => {
console.log("Normal" + this.name)
},100)
}
arrow(name);
func1(name);
}
x("BOB")
调用 x("BOB") 函数后如何从外部调用 func1 以及箭头函数与普通函数相比如何更改 this 的值
如果你想在外面调用箭头,你可以尝试返回函数并像这样调用(闭包概念)
function x(name){
this.name = name;
const arrow =() =>{
setTimeout(() => {
console.log("Arrow" + this.name)
},100)
}
const func1 = () => {
setTimeout(() => {
console.log("Normal" + this.name)
},100)
}
arrow(name);
func1(name);
return arrow
}
arrow = x("BOB")
arrow()
但我更喜欢,您尝试使用 OOP 结构。
我相信下面的例子正是你想要的:
function x(name) {
this.name = name;
const arrow = () => {
setTimeout(() => {
console.log("Arrow" + this.name);
}, 100);
};
const func1 = () => {
setTimeout(() => {
console.log("Normal" + this.name);
}, 100);
};
arrow(name);
func1(name);
x.func1 = func1;
x.arrow = arrow;
}
x("nawaf");
x.func1();
x.arrow();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.