Asp.net Ajax 客户端编程(一)——对JS基本类型的扩展
一:Array类型扩展
1:Array.add(array, item):在数组的末尾增加一个元素,静态方法,不需要建立实例即可调用:
//Demo :http://young18.cnblogs.com
var Arr=["Asp.net","Csharp","Ajax"];
Array.add(Arr, "Javascript");
alert(Str.toString()); //返回:Asp.net,Csharp,Ajax,Javascript
2:Array.addRange(arrayA, arrayB):将数组arrayB中的元素全部添加到数组arrayA后,静态方法.
//Demo :http://young18.cnblogs.com/
var ArrA=["Asp.net","Csharp"];
var ArrB=["Ajax","Javascript"];
Array.addRange(ArrA, ArrB);
alert(ArrA.toString()); //返回:Asp.net,Csharp,Ajax,Javascript
3:Array.clear(array):清除数组中的所有元素,静态方法.
//Demo:http://young18.cnblogs.com/
var Arr=["Asp.net","Csharp"];
Array.clear(Arr);
alert(Arr.toString()); //返回:""
4:Array.clone(array): 对数组array进行浅拷贝,静态方法。
//Demo:http://young18.cnblogs.com/
var ArrA=["Asp.net","Csharp"];
var ArrB=Array.clone(ArrA);
alert(ArrB.toString()); //返回:Asp.net,Csharp
5:Array.contains(array, item): 判断数组中是否包含元素,返回true或false,静态方法。
//Demo:http://young18.cnblogs.com/
var ArrA=["Asp.net","Csharp"];
var A=Array.contains(ArrA,"Csharp");
var B=Array.contains(ArrA,"Ajax");
alert("A =" + A.toString()); //返回:A = true
alert("B ="+ B.toString()); //返回:B = false
6:队列
Array.enqueue(array, item):在数组末尾添加一个元素,静态方法。
Array.dequeue(array); 移除数组的第一个元素,静态方法。
//Demo:http://young18.cnblogs.com/
var Arr=["Asp.net","Csharp"];
Array.enqueue(Arr,"Ajax");
alert("enqueue后:" + Arr.toString()); //返回: enqueue后: Asp.net,Csharp,Ajax
Array.dequeue(Arr);
alert("dequeue后:" + Arr.toString()); //返回: dequeue后: Csharp,Ajax
7:Array.forEach(array, method, context):对数组中的每一个元素执行指定方法,context为执行方法是被传入的内容,在函数中用this来获取,静态方法。
//Demo:http://young18.cnblogs.com/
var Arr=["Asp.net","Csharp","Ajax"];
Array.forEach(Arr, AlertForArr, 'Young is studying ');
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
function AlertForArr(element, index, array)
{
alert("Arr:" + array.toString() + "\n" + index + ":" + this + element);
//这里的this表示context的内容
}
8:Array.indexOf(array, item, start):在数组中查找指定元素,返回索引值,如果没找到,返回 -1,start指定从数组中那个位置开始查找。静态方法。
//Demo:http://young18.cnblogs.com/
var Arr=["Asp.net","Ajax","Csharp","Javascript"];
alert(Array.indexOf(Arr, "Ajax")); //返回: 1
alert(Array.indexOf(Arr, "Ajax",2)); //返回: -1
9:Array.insert(array, index, item):在数组中指定位置插入元素,静态方法。
//Demo:http://young18.cnblogs.com/
var Arr=["Asp.net","Csharp","Javascript"];
Array.insert(Arr, 2, "Ajax");
alert(Arr.toString()); //返回: Asp.net,Csharp,Ajax,Javascript
10:Array.parse(value):将字符串转化为数组,该字符串的格式有一定的限制,静态方法。
//Demo:http://young18.cnblogs.com/
var a = Array.parse("['Asp.net','Csharp','Javascript']");
alert(a.toString()); //返回: Asp.net,Csharp,Javascript
11:Array.remove(array, item):从数组中移除指定元素,静态方法。
Array.removeAt(array, index):从数组中移除指定元素,静态方法。
//Demo:http://young18.cnblogs.com/
var Arr=["Asp.net","Csharp","Ajax","Javascript"];
Array.remove(Arr,"Ajax");
alert(Arr.toString()); //返回: Asp.net,Csharp,Javascript
Array.removeAt(Arr,1);
alert(Arr.toString()); //返回: Asp.net,Javascript
二:Boolean类型扩展
Boolean.parse("true"); 将字符串转化为Bool类型。
//Demo:http://young18.cnblogs.com/
var b = Boolean.parse("true");
if (b == true){
alert("b = true");
}else{
alert("b = false");
}
三:Data类型扩展
1:dateVar.format(format): 日期类型格式化为字符串
2:dateVar.localeFormat(format);
3:Date.parseLocale(value, formats):将字符串转化为日期格式
4:Date.parseInvariant(value, formats):
//Demo:http://young18.cnblogs.com/
var dateVar=Date.parseLocale("2007年5月8日", "yyyy年M月d日");
alert(dateVar.toString()) //返回:Tue May 8 00:00:00 UTC+0800 2007
四:Error类型扩展
1:Error.argumentOutOfRange(paramName, actualValue, message) 创建一个范围异常,当参数超过指定范围时触发异常。
2:Error.argumentType(paramName, actualType, expectedType, message); 参数类型错误异常。
3:Error.invalidOperation(message); 参数操作错误异常
4:Error.parameterCount(message); 参数个数错误异常
5:errorInstanceVar.popStackFrame(); 创建实例错误时发生异常,与上面不同的是,异常在函数外抛出。
//Demo:http://young18.cnblogs.com/
var Student=function(name,age,sex){
if (arguments.length !== arguments.callee.length){//参数个数出现错误
var e = Error.create("参数个数错误");
e.popStackFrame(); //在函数外抛出
return e;
}else{
if(name==undefined){
var err = Error.argumentNull("name", "姓名不能为空.");
throw err;
}else{
this.name = name;
}
if(isNaN(age)){
var err = Error.create("年龄必须为数字");
throw err;
}else{
this.age = age;
}
if(sex!="男"&&sex!="女"){
var err = Error.argument("sex", "性别请填写‘男’或‘女’.");
throw err;
}else{
this.sex = sex;
}
}
}
var err=new Student(sname,25,"女","ajax");
if (err)
{
alert(err.message);
}
try
{
var sname;
var student=new Student(sname,25,"女");
//var student=new Student("young","ajax","男");
//var student=new Student("young",25,"male");
alert(student.name + ":" + student.age)
}
catch(e)
{
alert(e.message)
}
五: 实例类型扩展
1:Object.getType(instance) 获取对象实例类型
2:Object.getTypeName(instance); 获取对象实例类型名
var a = new Number(2);
alert(Object.getType(a))
alertObject.getTypeName(a))
六:字符串类型扩展
1:myString.endsWith(suffix) 是否以指定字符串结尾,返回true或false
2:myString.startsWith(prefix); 是否以指定字符串开始,返回true或false
3:myString.trim();
4:myString.trimEnd();
5:myString.trimStart(); 移除开头或结尾的空格
1:Array.add(array, item):在数组的末尾增加一个元素,静态方法,不需要建立实例即可调用:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2:Array.addRange(arrayA, arrayB):将数组arrayB中的元素全部添加到数组arrayA后,静态方法.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3:Array.clear(array):清除数组中的所有元素,静态方法.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4:Array.clone(array): 对数组array进行浅拷贝,静态方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5:Array.contains(array, item): 判断数组中是否包含元素,返回true或false,静态方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6:队列
Array.enqueue(array, item):在数组末尾添加一个元素,静态方法。
Array.dequeue(array); 移除数组的第一个元素,静态方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7:Array.forEach(array, method, context):对数组中的每一个元素执行指定方法,context为执行方法是被传入的内容,在函数中用this来获取,静态方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8:Array.indexOf(array, item, start):在数组中查找指定元素,返回索引值,如果没找到,返回 -1,start指定从数组中那个位置开始查找。静态方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9:Array.insert(array, index, item):在数组中指定位置插入元素,静态方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10:Array.parse(value):将字符串转化为数组,该字符串的格式有一定的限制,静态方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11:Array.remove(array, item):从数组中移除指定元素,静态方法。
Array.removeAt(array, index):从数组中移除指定元素,静态方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
二:Boolean类型扩展
Boolean.parse("true"); 将字符串转化为Bool类型。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
三:Data类型扩展
1:dateVar.format(format): 日期类型格式化为字符串
2:dateVar.localeFormat(format);
var dateVar=new Date();
alert(dateVar.format("yyyy年M月d日")); //返回:2007年5月8日
alert(dateVar.localeformat("yyyy年M月d日")); //返回:2007年5月8日
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3:Date.parseLocale(value, formats):将字符串转化为日期格式
4:Date.parseInvariant(value, formats):
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
四:Error类型扩展
1:Error.argumentOutOfRange(paramName, actualValue, message) 创建一个范围异常,当参数超过指定范围时触发异常。
2:Error.argumentType(paramName, actualType, expectedType, message); 参数类型错误异常。
3:Error.invalidOperation(message); 参数操作错误异常
4:Error.parameterCount(message); 参数个数错误异常
5:errorInstanceVar.popStackFrame(); 创建实例错误时发生异常,与上面不同的是,异常在函数外抛出。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
五: 实例类型扩展
1:Object.getType(instance) 获取对象实例类型
2:Object.getTypeName(instance); 获取对象实例类型名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
六:字符串类型扩展
1:myString.endsWith(suffix) 是否以指定字符串结尾,返回true或false
2:myString.startsWith(prefix); 是否以指定字符串开始,返回true或false
3:myString.trim();
4:myString.trimEnd();
5:myString.trimStart(); 移除开头或结尾的空格