Define one JavaScript class

A simple JavaScript class


function Complex(real, imaginary) {
if(isNaN(real) || isNaN(imaginary))
throw new TypeError();
this.r = real;
this.i = imaginary;
}



/*
The instance methods of a class are defined as function-valued properties
of the prototype object. The methods defined here are inherited by all
instances and provide the shared behavior of the class. Note that JavaScript
instance methods must use "this" keword to access the instance fields.
*/


Complex.prototype.add = function(that) {
return new Complex(this.r + that.r, this.i + that.i);
};


Complex.prototype.mul = function(that) {
return new Complex(this.r * that.r - this.i * that.i,
  this.r * that.i + this.i * that.r);
};


Complex.prototype.mag = function() {
return Math.sqrt(this.r * this.r + this.i * this.i);
};


Complex.prototype.neg = function() { return new Complex(-this.r, -this.i); };


Complex.prototype.toString = function() { return "{" + this.r + "," + this.i + "}"; };


Complex.prototype.equals = function(that) {
return that!=null && 
  that.constructor === Complex &&
  this.r === that.r && this.i === that.i;
};



/*
Class fields (such as constants) and class methods are defined as 
properties of the constructor.
Note that class methods do not generally use the "this" keyword:
they operate only on their arguments.
*/



//Here are some class fields that hold useful predefined complex numbers.
//Their names are uppercase to indicate that they are constants.
//(In ECMAScript 5, we could actuatlly make these properties read-only.)

Complex.ZERO = new Complex(0,0);
Complex.ONE = new Complex(1,0);
Complex.I = new Complex(0,1);



/ /This class method parses a string in the format returned by the toString
//instance method and returns a Complex object or throws a TypeError.

Complex.parse = function(s) {
try {
var m = Complex._format.exec(s);//Regular expression magic
return new Complex(parseFloat(m[1]), parseFloat(m[2]));
} catch (x) {
throw new TypeError("Can't parse '" + s + "' as a complex number.");
}
};



//A "private" class field used in Complex.parse() above.
//The underscore in its name indicates that it is intended for internal use and
//should not be considered part of the public API of this class.

Complex._format = /^\{([^,]+),([^}]+)\}$/;


We can use the code like this:
var c = new Complex(2, 3);
var d = new Complex(c.i, c.r);
c.add(d).toString();
Complex.parse(c.toString()).add(c.neg()).equals(Complex.ZERO)



Note:
JaveScript does not have the keyword "Private". This example uses typographical conventions
to provide hints that some properties (whose names are in capital letters) should not be changed
and that others (whose names begin with an underscore) should not be used outside of the class.
Private properties can be emulated using the local variables of a closure.
Constant properties are possible in ECMAScript 5.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值