我需要在不更改格式的情况下链接嵌套函数的帮助
我已经尝试过嵌套功能,但我无法在下一个功能中收到它其余函数一和二的结果返回this.a is undefined
但是我不想改变格式
const All = function () {
this.obj = (a, b) => {
this.a = a;
return this;
};
this.one = () => {
console.log(this.a); //this.a is un-defined here
return this;
};
this.two = () => {
console.log(this.a); //this.a is un-defined here
return this;
};
};
const all = new All();
all.obj({
1: all.one().two(),
});
回答如下:您正在将undefined
分配给this.a
。试试:
const All = function() {
this.one = (a, b) => {
this.a = a;
return this;
};
this.two = () => {
console.log(this.a); //this.a is un-defined here
return this;
};
this.three = () => {
console.log(this.a); //this.a is un-defined here
return this;
};
};
const all = new All();
all.one(1).two()