手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

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

首页 > Javascript >

 

COPY自一个网站,该网站声明此文章来自CSDN,代码没有仔细研读,但担心以后可能会用到。先MARK一下,做个记录。所以……

直接为我的博客加点料,原作者看到此文时,如觉不适,请通知删除。谢谢

JavaScript代码
  1. <script>   
  2. function Point2D(x, y)   
  3. {   
  4.  this.x = x;   
  5.  this.y = y;   
  6.  Point2D.prototype.quadrant = function()   
  7.  {   
  8.   if (x > 0 && y > 0) return "I";   
  9.   else if (x < 0 && y > 0) return "II";   
  10.   else if (x < 0 && y < 0) return "III";   
  11.   else if (x > 0 && y < 0) return "IV";   
  12.   else if (x == 0) return "x-axis";   
  13.   else if (y == 0) return "y-axis";   
  14.   else throw new Error();   
  15.  }   
  16.  Point2D.prototype.toVector = function()   
  17.  {   
  18.   return new Vector2D(x, y);   
  19.  }   
  20.  Point2D.prototype.distance = function() //求距离   
  21.  {   
  22.   if (arguments.length == 1 && arguments[0] instanceof Point2D)    
  23.   {   
  24.    return this._point_distance.apply(this, arguments);   
  25.   }   
  26.   else if (arguments.length == 1 && arguments[0] instanceof Vector2D)    
  27.   {   
  28.    return this._vector_distance.apply(this, arguments);   
  29.   }   
  30.   else  
  31.   {   
  32.    throw new Error("Argument Error!");   
  33.   }   
  34.  }   
  35.  Point2D.prototype._point_distance = function(p)  //求两点之间的距离(函数重载)   
  36.  {   
  37.   return (new Vector2D(p,this)).length();   
  38.  }   
  39.  Point2D.prototype._vector_distance = function(v)  //求点到向量的距离(函数重载)   
  40.  {   
  41.   var v1 = new Vector2D(this, v.start);   
  42.   var v2 = new Vector2D(this, v.end);   
  43.   
  44.   var area = Math.abs(v1.cross(v2));  //平行四边形面积 = v1 X v2 = |v1v2|sin(v1,v2)   
  45.      
  46.   return area / v.length();   //平行四边形面积除以底边长度即为点到向量的距离   
  47.  }   
  48. }   
  49. function Vector2D()   
  50. {   
  51.  if (arguments.length == 2 && arguments[0] instanceof Point2D && arguments[1] instanceof Point2D)   
  52.  {   
  53.   _point_point_Vector2D.apply(this, arguments);   
  54.  }   
  55.  else if (arguments.length == 2 && !isNaN(arguments[0]) && !isNaN(arguments[1]))   
  56.  {   
  57.   _double_double_Vector2D.apply(this, arguments);   
  58.  }   
  59.  else if (arguments.length == 4 && !isNaN(arguments[0]) && !isNaN(arguments[1])    
  60.   && !isNaN(arguments[2]) && !isNaN(arguments[3]))   
  61.  {   
  62.   _double_double_double_double_Vector2D.apply(this, arguments);   
  63.  }   
  64.  else  
  65.  {   
  66.   throw new Error("Argument Error!");   
  67.  }   
  68. }   
  69. function _point_point_Vector2D(p1, p2)      
  70. {   
  71.  this.start = p1;   
  72.  this.end = p2;   
  73.  Vector2D.prototype.length = function() //求向量的长度   
  74.  {   
  75.   return Math.sqrt(this.pond_x() * this.pond_x() + this.pond_y() * this.pond_y());   
  76.  }   
  77.  Vector2D.prototype.pond_x = function() //x方向分量   
  78.  {   
  79.   return this.start.x - this.end.x;   
  80.  }   
  81.  Vector2D.prototype.pond_y = function()   
  82.  {   
  83.   return this.start.y - this.end.y;   
  84.  }   
  85.  Vector2D.prototype.cross = function(v)   //求向量的交积 P1 X P2 = x1y2 - x2y1   
  86.  {   
  87.   return this.pond_x() * v.pond_y() - v.pond_x() * this.pond_y();   
  88.  }   
  89. }   
  90. function _double_double_Vector2D(x,y) //重载构造函数Vector2D   
  91. {   
  92.  this.pointPairs = new Array();   
  93.  this.pointPairs[0] = new Point2D(0, 0);   
  94.  this.pointPairs[1] = new Point2D(x, y);   
  95.   
  96.  _point_point_Vector2D.apply(thisthis.pointPairs);   
  97. }   
  98. function _double_double_double_double_Vector2D(x1, y1, x2, y2)  //重载构造函数Vector2D   
  99. {   
  100.  this.pointPairs = new Array();   
  101.  this.pointPairs[0] = new Point2D(x1, y1);   
  102.  this.pointPairs[1] = new Point2D(x2, y2);   
  103.   
  104.  _point_point_Vector2D.apply(thisthis.pointPairs);   
  105. }   
  106. var p1 = new Point2D(0,0);   
  107. var p2 = new Point2D(10,10);   
  108. var v1 = new Vector2D(p1,p2);  //通过两个点(p1,p2)的方式来构造向量V1   
  109. alert("向量v1长度:"+v1.length());   
  110. var v2 = new Vector2D(0,0,5,5);  //通过四个坐标(x1,y1,x2,y2)的方式来构造向量V2   
  111. alert("向量v2长度:"+v2.length());   
  112. var v3 = new Vector2D(0,10);  //通过指定终点的方式来构造向量V3   
  113. alert("向量v3长度:"+v3.length());   
  114. alert("向量v1与v2的交积:"+v1.cross(v2));  //求V1 X V2 (因为平行,所以结果为0)   
  115.   
  116. var p3 = new Point2D(10,0);   
  117. alert("点p1与p3的距离:"+p1.distance(p3));   
  118. alert("点p3与向量v1的距离:"+p3.distance(v1));   
  119. </script>  

 




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: js, 重载

« 上一篇 | 下一篇 »

只显示10条记录相关文章

javascipt的{}中逗号带来的困扰 (浏览: 29666, 评论: 6)
JS模拟FLASH效果 (浏览: 23225, 评论: 0)
Javascript中各种trim的实现 (浏览: 23101, 评论: 0)
IE和FF下JS的不同点(更详细) (浏览: 21742, 评论: 0)
JS 刷新页面的几个技巧 (浏览: 19736, 评论: 0)
对膘叔的文章感兴趣的朋友可以使用JS调用功能 (浏览: 19727, 评论: 0)
JS实现的街头霸王 (浏览: 18979, 评论: 1)
收藏的关于JS的一些文章 (浏览: 16936, 评论: 0)

发表评论

评论内容 (必填):