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

javascript - How to do typescript function chaining? - Stack Overflow

programmeradmin3浏览0评论

I want to use function chaining in typescript.

Consider a class

export class numbOp(){
  private n;
  constructor(int num){
    this.n = num;
  }

  public add(inc = 1){
    this.n = this.n + inc;
  }

}

How do I use it as (1)

let finalNumber = new numbOp(3);
console.log(finalNumber) // Output: 3

How do I use it as (2)

let finalNumber = new numbOp(3).add();
console.log(finalNumber) // Output: 4

How do I use it as (3)

let finalNumber = new numbOp(3).add().add();
console.log(finalNumber) // Output: 5

How do I use it as (4)

let finalNumber = new numbOp(3).add().add(2).toString();
console.log(finalNumber) // Output: "6"

Please, help me out to achieve this. Thanks in advance :)

I want to use function chaining in typescript.

Consider a class

export class numbOp(){
  private n;
  constructor(int num){
    this.n = num;
  }

  public add(inc = 1){
    this.n = this.n + inc;
  }

}

How do I use it as (1)

let finalNumber = new numbOp(3);
console.log(finalNumber) // Output: 3

How do I use it as (2)

let finalNumber = new numbOp(3).add();
console.log(finalNumber) // Output: 4

How do I use it as (3)

let finalNumber = new numbOp(3).add().add();
console.log(finalNumber) // Output: 5

How do I use it as (4)

let finalNumber = new numbOp(3).add().add(2).toString();
console.log(finalNumber) // Output: "6"

Please, help me out to achieve this. Thanks in advance :)

Share Improve this question edited Dec 29, 2018 at 23:54 Abhijit Srivastava asked Dec 29, 2018 at 23:39 Abhijit SrivastavaAbhijit Srivastava 1,4592 gold badges13 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You just need to return this from the functions you want to chain

class numbOp {
    private n: number;
    constructor(num: number) {
        this.n = num;
    }

    public add(inc = 1) : this { // annotation not necessary added to address ments
        this.n = this.n + inc;
        return this;
    }
    toString() { 
        return this.n;
    }

}
let finalNumber = new numbOp(3);
console.log(finalNumber + "") // Output: 3
//How do I use it as (2)
let finalNumber2 = new numbOp(3).add();
console.log(finalNumber2 + "") // Output: 4
//How do I use it as (3)
let finalNumber3 = new numbOp(3).add().add();
console.log(finalNumber3 + "") // Output: 5
//How do I use it as (4)
let finalNumber4 = new numbOp(3).add().add(2).toString();
console.log(finalNumber4) // Output: "6"

Edit

Since the console.log part seems to have bee more interesting then the chain part in the ments, I'll add the ways to ensure the output in the console is a number:

  1. Override toString and use string coercion to get the string representation of the object
  2. Require a terminator method be called before the display (ie don't forget to call toString when you finish the chain)
  3. Override valueOf and use the unary + operator (this will also make you class usable in binary operations

Code for last option:

class numbOp {
    private n: number;
    constructor(num: number) {
        this.n = num;
    }

    public add(inc = 1) : this { // annotation not necessary added to address ments
        this.n = this.n + inc;
        return this;
    }
    valueOf() { 
        return this.n;
    }

}
let finalNumber2 = new numbOp(3).add();
console.log(+finalNumber2) // Output: 4
console.log(1 + (+finalNumber2)) // Output: 5
console.log(1+(finalNumber2 as any as number)) // Output: 5
发布评论

评论列表(0)

  1. 暂无评论