《一》。利用arguments对象可以模拟函数重载
例:function addition(){
if(arguments.length == 1){
}else if (arguments.length == 2) {
}
}
调用:addition (args1),addition(args1,args2);
《二》。javascript继承实现的方式
a》。对象冒充
b》。原型链
c》。混合方式
javascript
//多边形
function Ploygon(iSides) {
this.sides = iSides;
//多边形的边数
}
Ploygon.prototype.getArea = function() {
return 0;
//占位符,以被子类覆盖
}
/**
*三角形
* @param {Object} iBase
* @param {Object} iHeight
*/
function Triangle(iBase, iHeight) {
Ploygon.call(this, 3);
//继承父类方法
this.base = iBase;
this.height = iHeight;
}
Triangle.prototype = new Ploygon();
//继承
//重写方法
Triangle.prototype.getArea = function() {
return 0.5 * this.base * this.height;
}
function Rectangle(iLenght, iWidth) {
Ploygon.call(this, 4);
this.length = iLenght;
this.width = iWidth;
}
Rectangle.prototype = new Ploygon();
Rectangle.prototype.getArea = function() {
return this.length * this.width;
}
function Ploygon(iSides) {
this.sides = iSides;
//多边形的边数
}
Ploygon.prototype.getArea = function() {
return 0;
//占位符,以被子类覆盖
}
/**
*三角形
* @param {Object} iBase
* @param {Object} iHeight
*/
function Triangle(iBase, iHeight) {
Ploygon.call(this, 3);
//继承父类方法
this.base = iBase;
this.height = iHeight;
}
Triangle.prototype = new Ploygon();
//继承
//重写方法
Triangle.prototype.getArea = function() {
return 0.5 * this.base * this.height;
}
function Rectangle(iLenght, iWidth) {
Ploygon.call(this, 4);
this.length = iLenght;
this.width = iWidth;
}
Rectangle.prototype = new Ploygon();
Rectangle.prototype.getArea = function() {
return this.length * this.width;
}