Posted on 2006-12-27 19:49
兵临城下 阅读(401)
评论(0) 编辑 收藏 所属分类:
JavaScript
************************************************************************************************************************************
对象创建:
var Acircle = {
pi : Math.PI,
getCircleArea: function(x,y,r) {
return this.pi*r*r;
}
}
上述与以下代码相同:
function Circle(xPoint,yPoint,radius) {
this.x = xPoint;
this.y = yPoint;
this.r = radius;
}
Circle.prototype.pi = Math.PI;
function Circle.prototype.area() {
return this.pi*this.r*this.r;
}
//Circle.prototype.area = circleArea;
function getCircleArea(x,y,r) {
aCircle = new Circle(x,y,r);
return aCircle.area();
}
===========================================================
var Try = {
these: function() {
var returnValue;
for (var i = 0; i < arguments.length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch (e) {}
}
return returnValue;
}
}
var Ajax = {
getTransport: function() {
return Try.these(
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
) || false;
},
activeRequestCount: 0
}
================================================================
用原型方法(用prototype定义)在大量定义对象的时候会比较节省资源。也可以在系统的类(如String)上面附加新的方法。
function String.prototype.trim()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
alert(" abcd ".trim());