Skip to main content
added 162 characters in body
Source Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 100

If you want to use object-oriented javascript for this calculator, I would prefer the class syntax over the prototype syntax.

This prototype approach ..

function Calculate(num1, num2, op) {
      this.num1 = num1;
      this.num2 = num2;
      this.op = op;
    } 

    Calculate.prototype.result = function() {
       // ..
    }

.. can be replaced by (Fiddle):

class BinaryExpression {
    constructor(a, b, op) {
        this.a = a;
        this.b = b;
        this.op = op;
    }
    evaluate() {
        switch (this.op) {
          case 'add':
            return this.a + this.b;
          case 'sub':
            return this.a - this.b;
          case 'mul':
            return this.a * this.b;
          case 'div':
            return this.a / this.b;
          default:
            return 'Error! No operation selected.';
        }
    }
}

###Misc

  • Most math expression compilers would implement a switch for the specific binary operators, but you could take it a step further and sub-class each binary operation.
  • Rather than returning a default message when the operator is not known, you could throw an exception and let the consumer handle it.

If you want to use object-oriented javascript for this calculator, I would prefer the class syntax over the prototype syntax.

This prototype approach ..

function Calculate(num1, num2, op) {
      this.num1 = num1;
      this.num2 = num2;
      this.op = op;
    } 

    Calculate.prototype.result = function() {
       // ..
    }

.. can be replaced by (Fiddle):

class BinaryExpression {
    constructor(a, b, op) {
        this.a = a;
        this.b = b;
        this.op = op;
    }
    evaluate() {
        switch (this.op) {
          case 'add':
            return this.a + this.b;
          case 'sub':
            return this.a - this.b;
          case 'mul':
            return this.a * this.b;
          case 'div':
            return this.a / this.b;
          default:
            return 'Error! No operation selected.';
        }
    }
}

If you want to use object-oriented javascript for this calculator, I would prefer the class syntax over the prototype syntax.

This prototype approach ..

function Calculate(num1, num2, op) {
      this.num1 = num1;
      this.num2 = num2;
      this.op = op;
    } 

    Calculate.prototype.result = function() {
       // ..
    }

.. can be replaced by (Fiddle):

class BinaryExpression {
    constructor(a, b, op) {
        this.a = a;
        this.b = b;
        this.op = op;
    }
    evaluate() {
        switch (this.op) {
          case 'add':
            return this.a + this.b;
          case 'sub':
            return this.a - this.b;
          case 'mul':
            return this.a * this.b;
          case 'div':
            return this.a / this.b;
          default:
            return 'Error! No operation selected.';
        }
    }
}

###Misc

  • Most math expression compilers would implement a switch for the specific binary operators, but you could take it a step further and sub-class each binary operation.
  • Rather than returning a default message when the operator is not known, you could throw an exception and let the consumer handle it.
Source Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 100

If you want to use object-oriented javascript for this calculator, I would prefer the class syntax over the prototype syntax.

This prototype approach ..

function Calculate(num1, num2, op) {
      this.num1 = num1;
      this.num2 = num2;
      this.op = op;
    } 

    Calculate.prototype.result = function() {
       // ..
    }

.. can be replaced by (Fiddle):

class BinaryExpression {
    constructor(a, b, op) {
        this.a = a;
        this.b = b;
        this.op = op;
    }
    evaluate() {
        switch (this.op) {
          case 'add':
            return this.a + this.b;
          case 'sub':
            return this.a - this.b;
          case 'mul':
            return this.a * this.b;
          case 'div':
            return this.a / this.b;
          default:
            return 'Error! No operation selected.';
        }
    }
}