我在 java-script 中使用原型创建了一个复杂函数的乘法
function ComplexNumber(real,imaginary) {
this.real = real;
this.imaginary = imaginary;
}
ComplexNumber.prototype = {
mult: function() {
var multiplier = arguments[0];
if(arguments.length != 1)
multiplier = new ComplexNumber(arguments[0], arguments[1]);
return new ComplexNumber(this.real * multiplier.real - this.imaginary * multiplier.imaginary,
this.real * multiplier.imaginary + this.imaginary * multiplier.real);
},
toString: function() {
return this.real + " + " + this.imaginary + "j";
}
},
var complexA = new ComplexNumber(5, 8),
complexB = new ComplexNumber(3, 4),
complexSum = complexA.mult(complexB);
console.log(complexSum.toString());
我必须创建我尝试过的复数除法,但它不起作用......以下是我的 div 函数:
div : function(){
var multiplier = arguments[0];
if(arguments.length != 1)
multiplier = new ComplexNumber(arguments[0], arguments[1]);
return new ComplexNumber((this.real * multiplier.real - this.imaginary * multiplier.imaginary,
this.real * multiplier.imaginary + this.imaginary * multiplier.real)/( ((this.real)*(this.real))) +((this.multiplier)*(this.multiplier)));
},