Define one JavaScript class

來源:互聯網
上載者:User

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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.