最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

运维笔记admin12浏览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()
发布评论

评论列表(0)

  1. 暂无评论