javascript实现函数重载的深入探索(实例)

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 yanwl 的 blog

<script>
function point2d(x, y)
{
 this.x = x;
 this.y = y;
 point2d.prototype.quadrant = function()
 {
  if (x > 0 && y > 0) return "i";
  else if (x < 0 && y > 0) return "ii";
  else if (x < 0 && y < 0) return "iii";
  else if (x > 0 && y < 0) return "iv";
  else if (x == 0) return "x-axis";
  else if (y == 0) return "y-axis";
  else throw new error();
 }
 point2d.prototype.tovector = function()
 {
  return new vector2d(x, y);
 }
 point2d.prototype.distance = function() //求距离
 {
  if (arguments.length == 1 && arguments[0] instanceof point2d)
  {
   return this._point_distance.apply(this, arguments);
  }
  else if (arguments.length == 1 && arguments[0] instanceof vector2d)
  {
   return this._vector_distance.apply(this, arguments);
  }
  else
  {
   throw new error("argument error!");
  }
 }
 point2d.prototype._point_distance = function(p)  //求两点之间的距离(函数重载)
 {
  return (new vector2d(p,this)).length();
 }
 point2d.prototype._vector_distance = function(v)  //求点到向量的距离(函数重载)
 {
  var v1 = new vector2d(this, v.start);
  var v2 = new vector2d(this, v.end);

  var area = math.abs(v1.cross(v2));  //平行四边形面积 = v1 x v2 = |v1v2|sin(v1,v2)
  
  return area / v.length();   //平行四边形面积除以底边长度即为点到向量的距离
 }
}
function vector2d()
{
 if (arguments.length == 2 && arguments[0] instanceof point2d && arguments[1] instanceof point2d)
 {
  _point_point_vector2d.apply(this, arguments);
 }
 else if (arguments.length == 2 && !isnan(arguments[0]) && !isnan(arguments[1]))
 {
  _double_double_vector2d.apply(this, arguments);
 }
 else if (arguments.length == 4 && !isnan(arguments[0]) && !isnan(arguments[1])
  && !isnan(arguments[2]) && !isnan(arguments[3]))
 {
  _double_double_double_double_vector2d.apply(this, arguments);
 }
 else
 {
  throw new error("argument error!");
 }
}
function _point_point_vector2d(p1, p2)  
{
 this.start = p1;
 this.end = p2;
 vector2d.prototype.length = function() //求向量的长度
 {
  return math.sqrt(this.pond_x() * this.pond_x() + this.pond_y() * this.pond_y());
 }
 vector2d.prototype.pond_x = function() //x方向分量
 {
  return this.start.x - this.end.x;
 }
 vector2d.prototype.pond_y = function()
 {
  return this.start.y - this.end.y;
 }
 vector2d.prototype.cross = function(v)   //求向量的交积 p1 x p2 = x1y2 - x2y1
 {
  return this.pond_x() * v.pond_y() - v.pond_x() * this.pond_y();
 }
}
function _double_double_vector2d(x,y) //重载构造函数vector2d
{
 this.pointpairs = new array();
 this.pointpairs[0] = new point2d(0, 0);
 this.pointpairs[1] = new point2d(x, y);

 _point_point_vector2d.apply(this, this.pointpairs);
}
function _double_double_double_double_vector2d(x1, y1, x2, y2)  //重载构造函数vector2d
{
 this.pointpairs = new array();
 this.pointpairs[0] = new point2d(x1, y1);
 this.pointpairs[1] = new point2d(x2, y2);

 _point_point_vector2d.apply(this, this.pointpairs);
}
var p1 = new point2d(0,0);
var p2 = new point2d(10,10);
var v1 = new vector2d(p1,p2);  //通过两个点(p1,p2)的方式来构造向量v1
alert("向量v1长度:"+v1.length());
var v2 = new vector2d(0,0,5,5);  //通过四个坐标(x1,y1,x2,y2)的方式来构造向量v2
alert("向量v2长度:"+v2.length());
var v3 = new vector2d(0,10);  //通过指定终点的方式来构造向量v3
alert("向量v3长度:"+v3.length());
alert("向量v1与v2的交积:"+v1.cross(v2));  //求v1 x v2 (因为平行,所以结果为0)

var p3 = new point2d(10,0);
alert("点p1与p3的距离:"+p1.distance(p3));
alert("点p3与向量v1的距离:"+p3.distance(v1));
</script>

本文关键:javascript实现函数重载的深入探索(实例)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top