用Javascript创建科学计算器

用Javascript创建科学计算器,javascript,Javascript,我有一个科学计算器,我有一个计算器。我的问题是如何编写符合此规范的科学计算器类 describe( "ScientificCalculator", function(){ var calculator; beforeEach( function(){ calculator = new ScientificCalculator(); } ); it( "extends Calculator", function(){ expect( calculator ).to.be.instan

我有一个科学计算器,我有一个计算器。我的问题是如何编写符合此规范的科学计算器类

describe( "ScientificCalculator", function(){
 var calculator;
 beforeEach( function(){
 calculator = new ScientificCalculator();
 } 
);

it( "extends Calculator", function(){
 expect( calculator ).to.be.instanceOf( Calculator );
 expect( calculator ).to.be.instanceOf( ScientificCalculator );
 } 
);

it( "returns the sine of PI / 2", function(){
 expect( calculator.sin( Math.PI / 2 ) ).to.equal( 1 );
 } 
);

it( "returns the cosine of PI", function(){
 expect( calculator.cos( Math.PI ) ).to.equal( -1 );
 } 
);

it( "returns the tangent of 0", function(){
 expect( calculator.tan( 0 ) ).to.equal( 0 );
 } 
);

it( "returns the logarithm of 1", function(){
 expect( calculator.log( 1 ) ).to.equal( 0 );
 } 
);
} 
);
似乎是一个标准。 您可以使用类似于下面的方法来实现所需的效果。 根据OPs新数据编辑代码

//Define a calculator base class
var Calculator = function(op1, op2) { 
  this.add = function(op1, op2) { return op1 + op2; }; 
  this.multiply = function(op1, op2) { return op2 * op1; }; 
  this.subtract = function(op1, op2) { return op1 - op2; }; 
  this.divide = function(op1, op2) { 
    if (op1/op2 === Infinity) {
      return Infinity - Infinity; 
    } else return op1/op2; 
  }; 
};


//define a sub class scientific calculator
function ScientificCalculator() {}
ScientificCalculator.prototype = new Calculator();
ScientificCalculator.prototype.constructor = Calculator;
//Add methods to the base class
ScientificCalculator.prototype.sin = function() {
  //Your imlementation here for Sin, repeat same for cos and others
};
var sc = new ScientificCalculator();

假设您已经在某个地方定义了计算器类,您可以尝试以下操作:

function ScientificCalculator() {
  // whatever you want, private variable declarations perhaps?
}

ScientificCalculator.prototype = Object.create(Calculator.prototype);
ScientificCalculator.prototype.constructor = Calculator;

ScientificCalculator.prototype.sin = function(x) {
  return Math.sin(x);
}

ScientificCalculator.prototype.cos = function(x) {
  return Math.cos(x);
}

ScientificCalculator.prototype.tan = function(x) {
  return Math.tan(x);
}

ScientificCalculator.prototype.log = function(x) {
  return Math.log(x);
}

如果您不提供至少一些您尝试编写的代码,您将不会得到太多帮助。此代码用于科学计算器。我用Javascript编写了自己的计算器类。但是,问题是如何完成上述规范。请发布您的ScientificCalculator类,并解释您所说的“完成上述规范”是什么意思。以上是科学计算器的不完整代码。我需要完成上述规范,以便使其运行var Calculator=function(op1,op2){this.add=function(op1,op2){return op1+op2;};this.multiply=function(op1,op2){return op2*op1;};this.subtract=function(op1,op2){return op1-op2;};this.divide=function(op1,op2){if(op1/op2===Infinity){return Infinity-Infinity;}否则返回op1/op2;};};};这是计算器基类。我需要这样做吗:scientificalculator.prototype.sin=function(x){return Math.sin(x);};scientificalculator.prototype.cos=function(x){return Math.cos(x);};是的。