Search

1/03/2011

Performance Calendar » Coding Better Object-Oriented JavaScript with Closure Compiler

Performance Calendar » Coding Better Object-Oriented JavaScript with Closure Compiler


/**
* Helper function that implements (pseudo)Classical inheritance inheritance.
* @see http://www.yuiblog.com/blog/2010/01/06/inheritance-patterns-in-yui-3/
* @param {Function} childClass
* @param {Function} parentClass
*/
function inherits(childClass, parentClass) {
/** @constructor */
var tempClass = function() {
};
tempClass.prototype = parentClass.prototype;
childClass.prototype = new tempClass();
childClass.prototype.constructor = childClass;
}

//////////////////////////////////////////////////////////////////////////////

/**
* The shape
* @constructor
*/
function Shape() {
// No implementation.
}

/**
* Get the size
* @return {number} The size.
*/
Shape.prototype.getSize = function() {
// No implementation.
};

//////////////////////////////////////////////////////////////////////////////

/**
* The Box.
* @param {number} width The width.
* @param {number} height The height.
* @constructor
* @extends {Shape}
*/
function Box(width, height) {
Shape.call(this);

/**
* @private
* @type {number}
*/
this.width_ = width;

/**
* @private
* @type {number}
*/
this.height_ = height;
}
inherits(Box, Shape);

/**
* @return {number} The width.
*/
Box.prototype.getWidth = function() {
return this.width_;
};

/**
* @return {number} The height.
*/
Box.prototype.getHeight = function() {
return this.height_;
};

/** @inheritDoc */
Box.prototype.getSize = function() {
return this.height_ * this.width_;
};

////////////////////////////////////////////////////////////////////////////

/**
* The Box.
* @param {number} width The width.
* @param {number} height The height.
* @param {number} depth The depth.
* @constructor
* @extends {Box}
*/
function Cube(width, height, depth) {
Box.call(this, width, height);

/**
* @private
* @type {number}
*/
this.depth_ = depth;
}
inherits(Cube, Box);

/**
* @return {number} The width.
*/
Cube.prototype.getDepth = function() {
return this.depth_;
};

/** @inheritDoc */
Cube.prototype.getSize = function() {
return this.depth_ * this.getHeight() * this.getWidth();
};

////////////////////////////////////////////////////////////////////////////

var cube = new Cube(3, 6, 9);
document.write(cube.getSize().toString());



// skip example code.

////////////////////////////////////////////////////////////////////////////

/**
* The shape
* @interface
*/
function Shape() {
}

/**
* Get the size
* @return {number} The size.
*/
Shape.prototype.getSize = function() {};

////////////////////////////////////////////////////////////////////////////

/**
* The Box.
* @param {number} width The width.
* @param {number} height The height.
* @constructor
* @implements {Shape}
*/
function Box(width, height) {
Shape.call(this);

/**
* @private
* @type {number}
*/
this.width_ = width;

/**
* @private
* @type {number}
*/
this.height_ = height;
}

/**
* @return {number} The width.
*/
Box.prototype.getWidth = function() {
return this.width_;
};

// skip example code

沒有留言: