Javascript对象扩展 - JsPoint类

在Javascript程序设计中,时常会用到或新增自己编写的各类函数,随着此类小函数的数量的增加,管理上会比较繁重,如果把基本功能分类封装到各种既有对象或自定义类中去,如Object,Math,String,Number等等,使用的时候会非常的方便,并且更容易理解和管理。

现在已经实现的扩展有Math简单数学函数、模拟实现C++的CPoint的JsPoint类、模拟实现C++的CRect的JsRect矩形类、模拟实现C++的CRgn的JsRange类、对于线条的实现JsLine类等,将会陆续贴出。

下面是自定义的JsPoint类,实现了C++的CPoint类,在画图和计算中应用,本类外部调用了扩展后的Object.IsPoint():


/* - ==========================================================
*     开发人员:卢印刚
*     编写时间:2006-9-26
*     函数名称:JsPoint
*     参数说明:
*     功能说明:对C++的CPoint类的实现
*     使用说明: 1、JsPoint( x, y ),根据x,y构造新的对象,最基本的构造方式
*                            2、JsPoint.Clone(),复制当前的JsPoint对象
*                            3、JsPoint.Copy( JsPoint ),复制JsPoint对象对象到当前
*                            4、JsPoint.SetPoint( x, y ),将x,y赋值到当前
*                            5、JsPoint.Offset( x, y ),将JsPoint对象进行Offset操作,分别加上x和y
*                            6、JsPoint.Equal( JsPointNew ),判断当前的对象是否与新的JsPointNew对象相等
*                            7、JsPoint.Add( JsPointNew ),实现了 + 操作附,并返回新的值
*                            8、JsPoint.Sub( JsPointNew ),实现了 - 操作附,并返回新的值
*                            9、JsPoint.AddEv( JsPointNew ),实现了 += 操作符
*                            10、JsPoint.SubEv( JsPointNew ),实现了 -= 操作符
-
*/
{
function  JsPoint()
{
    
this .x  =   this .y  =   0 ;
    
if  ( arguments.length  >=   2 ){
        
this .x  =  isNaN( arguments[ 0 ] )  ?   0  : arguments[ 0 ];
        
this .y  =  isNaN( arguments[ 1 ] )  ?   0  : arguments[ 1 ];
    }
    
this .version  =   function (){
      alert( 
" JsPoint1.0版\n\n作者:卢印刚\n\n2006.9.26\n\n版权所有 " );
    }
}
JsPoint.prototype.Clone    
=   function ()        {    
      
return  (  new    this .constructor(  this .x,  this .y ) );        
}
JsPoint.prototype.Copy    
=   function ( JsPt )    {    
      
if  ( JsPt.IsPoint() ){ 
            
this .constructor( JsPt.x, JsPt.y ); 
      }    
}
JsPoint.prototype.SetPoint 
=   function ( x, y )    {    
      
if  ( isNaN(x)  ||  isNaN(y) )  return ;    
      
this .x  =  x;    
      
this .y  =  y;    
}
JsPoint.prototype.Offset    
=   function ( x, y )    {    
      
if  ( isNaN(x)  ||  isNaN(y) )  return ;    
      
this .x  +=  x;    
      
this .y  +=  y;    
}
JsPoint.prototype.Equal    
=   function ( JsPt )    {    
      
return  (  this .x  ==  JsPt.x )  &&  (  this .y  ==  JsPt.y );        
}
JsPoint.prototype.AddEv    
=   function ( JsPt )    {    
      
try {    
            
this .Addset( JsPt.x, JsPt.y );    
      }
catch (e){ }        
}
JsPoint.prototype.SubEv    
=   function ( JsPt )    {
      
try
            
this .Subset( JsPt.x, JsPt.y );
      }
catch (e){ }        
}
JsPoint.prototype.Add    
=   function ( JsPt )    { 
      
try
            
var  PT  =   this .Clone();
            PT.Offset( JsPt.x, JsPt.y );
            
return  PT;
      }
catch (e){ return   null ;}
}
JsPoint.prototype.Sub    
=   function ( JsPt )    { 
      
try
            
var  PT  =   this .Clone();
            PT.Offset( 
- JsPt.x,  - JsPt.y );
            
return  PT;
      }
catch (e){ return   null ;}
}

转载于:https://www.cnblogs.com/wait4u/archive/2006/09/27/516104.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值