现在已经实现的扩展有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 ;}
}