发新话题
打印

用oop的方法设计js脚本

用oop的方法设计js脚本

用oop的方法设计js脚本
基本概念:
JScript 是一种解释型的、基于对象的脚本语言。不能使用该语言来编写独立运行的应用程序,只能在某个解释器或“宿主”上运行,如 Active Server Pages(ASP)、Internet 浏览器或者 Windows 脚本宿主。
JScript 是一种宽松类型的语言,JScript 将根据需要自动进行转换。
Jscript 支持四种类型的对象:内置对象、生成的对象、宿主给出的对象(如 Internet 浏览器中的 window 和 document)以及 ActiveX 对象(外部组件)。
内置对象:ActiveXObject、Array、Boolean、Date、Function、Global、Math、Number、Object、RegExp以及String对象;还有Error、arguments、Enumerator、正则表达式对象、VBArray、Dictionary、FileSystemObject、TextSream对象的说法,由于后者要求js版本比较高,而且不常用到,所以此处不作解释。
a)       *ActiveXObject:启用并返回 Automation对象的引用。
þ        属性:无;
þ        方法:无;
þ         例子:var outXml=new ActiveXObject("Microsoft.XMLdom");
b)       Array:提供对创建任何数据类型的数组的支持。
þ        属性:constructor,length,prototype;
þ        方法:concat,join,reverse,slice,sort,toLocaleString,toString,valueOf;
þ        例子:
var my_array = new Array();
for (i = 0; i < 10; i++){
my_array = i;
}
x = my_array[4];
c)       *Boolean:创建新的Boolean值。
þ        属性:constructor,prototype;
þ        方法:toString,valueOf;
þ        例子:
d)       Date:启用基本存储器并取得日期和时间。
þ        属性:constructor,prototype;
þ        方法:getDate,getDay,getFullYear,getHours,getMilliseconds,getMinutes,getMonth,getSeconds,getTime,getTimezoneOffset,getUTCDate,getUTCDay,getUTCFullYear,getUTCHours,getUTCMilliSeconds,getUTCMinutes,getUTCMonth,getUTCSeconds,getVarDate,getYear,setDate,setFullYear,setHours,setMilliSeconds,setMinutes,setMonth,setSeconds,setTime,setUTCDate,setUTCFullYear,setUTCHours,setUTCMilliseconds,setUTCMinutes,setUTCMonth,setUTCSeconds,setYear,toGMTString,toLocaleString,toUTCString,toString,valueOf;静态方法(parse,UTC);
þ        例子:
e)       *Function:创建新的函数。
þ        属性:arguments,caller,constructor,prototype;
þ        方法:toString,valueOf;
þ        例子:
f)         Global:是一个内部对象,目的是把所有全局方法集中在一个对象中。Global 对象没有语法。直接调用其方法。
þ        属性:Infinity,NaN;
þ        方法:escape,eval,isFinite,isNaN,parseFloat,parseInt,unescape;
þ        例子:
g)       *Math:是一个内部对象,提供基本数学函数和常数。
þ        属性:E,LN2,LN10,LOG2E,LOG10E,PI,SQRT1_2,SQRT2;
þ        方法:静态方法(abs,acos,asin,atan,atan2,ceil,cos,exp,floor,log,max,min,pow,random,round,sin,sqrt,tan);
þ        例子:
h)       Number:代表数值数据类型和提供数值常数的对象。
þ        属性:MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,constructor,prototype;
þ        方法:toString,valueOf,toLocaleString;
þ        例子:
i)          Object:提供所有JScript对象通用的功能。
þ        属性:constructor,prototype;
þ        方法:toString,valueOf,toLocaleString;
þ        例子:
j)          RegExp:保存有关正则表达式模式匹配信息的固有全局对象。
þ        属性:$1...$9,index,input,lastIndex;
þ        方法:无;
þ        例子:
k)       *String:可用于处理或格式化文本字符串以及确定和定位字符串中的子字符串。
þ        属性:constructor,prototype,length;
þ        方法:anchor,big,blink,bold,charAt,charCodeAt,concat,fixed,fontcolor,fontsize,fromCharCode,indexOf,italics,lastIndexOf,link,match,replace,search,slice,small,split,strike,sub,substr,substring,sup,toLowerCase,toUpperCase,toString,valueOf;;
þ        例子:
注:*为页面中常用的内置对象。
创建自己的对象:
//----------------------------例子1-----------------------------------------
function Circle (xPoint, yPoint, radius) {
    this.x = xPoint;  // 圆心的 x 坐标。
    this.y = yPoint;  // 圆心的 y 坐标。
    this.r = radius;  // 圆的半径。
    this.pi=Math.PI;
    Circle.prototype.area=function(){
       return this.pi * this.r * this.r;
    }
}

function window_onload() {
       var aCircle = new Circle(12,12,2);
       alert(aCircle.area());
}

//----------------------------例子2-----------------------------------------
Object.prototype.x=0;
Object.prototype.y=0;
Object.prototype.r=1;
Object.prototype.pi=Math.PI;
Object.prototype.area=function(){
       return this.pi * this.r * this.r;
}
Object.prototype.Create=function(xPoint,yPoint,radius){
       this.x = xPoint;  // 圆心的 x 坐标。
           this.y = yPoint;  // 圆心的 y 坐标。
           this.r = radius;  // 圆的半径。
}
function window_onload() {
       var aCircle = new Object();
       aCircle.Create(12,12,2);

TOP

发新话题