最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

我需要在不更改格式的情况下链接嵌套函数的帮助

IT培训 admin 6浏览 0评论

我需要在不更改格式的情况下链接嵌套函数的帮助

我已经尝试过嵌套功能,但我无法在下一个功能中收到它其余函数一和二的结果返回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()

我需要在不更改格式的情况下链接嵌套函数的帮助

我已经尝试过嵌套功能,但我无法在下一个功能中收到它其余函数一和二的结果返回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()
发布评论

评论列表 (0)

  1. 暂无评论