-1

I have the following toy class to encapsulate some bitwise functions:

class BitOp {
    static set(num) {
        return num;
    }
    static shiftR(num, n=1) {
        return num >> n;
    }
    static shiftL(num, n=1) {
        return num << n;
    }
    static toBin(num) {
        console.log(num, '>', num.toString(2));
        return num;
    }
}

const
    set     = BitOp.set,
    shiftR  = BitOp.shiftR,
    shiftL  = BitOp.shiftL,
    toBin   = BitOp.toBin;
    
toBin(shiftL(set(44), 2));

I would like to see if I can use the above function and perhaps add a wrapper around a variable that calls it so that it can be used alternately to chain to gether method calls, such as like:

new BitOp().set(44).shiftL(2).toBin(); // or new BitOp(44).shiftL...

I know I can write a separate class for it, such as the following but I'd like to use a more compact and interesting approach!

class BitOp {
    set(num) {
        this.num = num;
        return this;
    }
    shiftR(n=1) {
        this.num >>= n;
        return this;
    }
    shiftL(n=1) {
        this.num <<= n;
        return this;
    }
    toBin() {
        console.log(this.num, '>', this.num.toString(2));
        return this;
    }
}

new BitOp().set(44).shiftL(2).toBin();
David542
  • 104,438
  • 178
  • 489
  • 842
  • 3
    You haven't asked a question to which you didn't already provide an own answer. – jsejcksn Mar 04 '22 at 23:15
  • @jsejcksn I'm seeing if there is something like this that can be done: `InlineWrapper = (n) => new BitOp(n);` -- I've seen the answer before I just can't find it. – David542 Mar 04 '22 at 23:16
  • @David542 So what exactly is the chained method call supposed to look like? Really not sure where exactly you’re stuck. – Sebastian Simon Mar 04 '22 at 23:17
  • 1
    @SebastianSimon maybe this was the technique I was thinking of: https://stackoverflow.com/a/70780793/651174 – David542 Mar 04 '22 at 23:20
  • @SebastianSimon I've added an answer which I think addresses this now. – David542 Mar 04 '22 at 23:35
  • 1
    "*I'd like to use a more compact and interesting approach!*" is not a good question for StackOverflow. I mean, it's not a question at all, it's the start of a discussion. – Bergi Mar 04 '22 at 23:36

2 Answers2

1

It sounds like you're asking how to instantiate a new class by invoking a function:

class BitOp {
  constructor (num) {
    this.num = num;
  }

  set (num) {
      this.num = num;
      return this;
  }

  shiftR (n=1) {
      this.num >>= n;
      return this;
  }

  shiftL (n=1) {
      this.num <<= n;
      return this;
  }

  toBin () {
      console.log(this.num, '>', this.num.toString(2));
      return this;
  }
}

function bitOp (num) {
  return new BitOp(num);
}

bitOp(5).toBin(); // logs 5 > 101
jsejcksn
  • 27,667
  • 4
  • 38
  • 62
0

Drawing inspiration from the accepted answer at Possible to do a chained factorial method without a helper method, here is an approach that would allow both:

const shiftR = (num, places) => num >> (places || 1);
const shiftL = (num, places) => num << (places || 1);
const BitOp = (num) => new InlineBitOp(num);

class InlineBitOp {
    constructor(num)    { this.num = num }
    shiftL(places)      { return BitOp(shiftL(this.num, places)) }
    shiftR(places)      { return BitOp(shiftR(this.num, places)) }
    toString()          { return this.num };
}

let x = 44;
console.log(shiftL(shiftL(x, 2), 3));
console.log('' + new InlineBitOp(x).shiftL(2).shiftL(3));
David542
  • 104,438
  • 178
  • 489
  • 842