当前页面: 开发资料首页 → Javascript 专题 → 用oop的方法设计js脚本
摘要: 用oop的方法设计js脚本
用oop的方法设计js脚本<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
JScript 是一种解释型的、基于对象的脚本语言。不能使用该语言来编写独立运行的应用程序,只能在某个解释器或“宿主”上运行,如 Active Server Pages(ASP)、Internet 浏览器或者 Windows 脚本宿主。
JScript 是一种宽松类型的语言,JScript 将根据需要自动进行转换。
Jscript 支持四种类型的对象:内置对象、生成的对象、宿主给出的对象(如 Internet 浏览器中的 window 和 document)以及 ActiveX 对象(外部组件)。
þ 属性:无;
þ 方法:无;
þ 例子:var outXml=new ActiveXObject("Microsoft.XMLdom");
þ 属性: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] = i;
}
x = my_array[4];
þ 属性:constructor,prototype;
þ 方法:toString,valueOf;
þ 例子:
þ 属性: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);
þ 例子:
þ 属性:arguments,caller,constructor,prototype;
þ 方法:toString,valueOf;
þ 例子:
þ 属性:Infinity,NaN;
þ 方法:escape,eval,isFinite,isNaN,parseFloat,parseInt,unescape;
þ 例子:
þ 属性: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);
þ 例子:
þ 属性:MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,constructor,prototype;
þ 方法:toString,valueOf,toLocaleString;
þ 例子:
þ 属性:constructor,prototype;
þ 方法:toString,valueOf,toLocaleString;
þ 例子:
þ 属性:$1...$9,index,input,lastIndex;
þ 方法:无;
þ 例子:
þ 属性: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);
alert(aCircle.area());
}
</td> </tr> <tr> <td vAlign=top align=left height="100%">