Ajax学习笔记(1)对js的扩展Array,Boolean,Date,Object,String
2009-06-16 18:39 key_sky 阅读(583) 评论(2) 编辑 收藏 举报Array:
Code
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Array Extensions </title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<div id="divAddRange">
</div>
<div id="divAdd">
</div>
<div id="divClear">
</div>
<div id="divContain">
</div>
<div id="divDequeue">
</div>
<div id="divForEach">
</div>
<div id="divIndexOf">
</div>
<div id="divInsert">
</div>
<div id="divRemoveItem">
</div>
<div id="divRemoveIndex">
</div>
<script type="text/javascript">
var arrDefault=['k','e','y','_','s','k','y'];
var arrChi=['H','e','l','l','o'];
Array.addRange(arrDefault,arrChi);
//将数组arrchi中的项依次添加到数组arrDefault的结尾
var sb=new Sys.StringBuilder();
for(index=0;index<arrDefault.length;++index){
sb.append(arrDefault[index]);
}
$get("divAddRange").innerHTML=sb.toString();
var str='!';
Array.add(arrDefault,str);
//将字符串str添加到数组arrDefault的末尾
var sbadd=new Sys.StringBuilder();
for(index=0;index<arrDefault.length;++index){
sbadd.append(arrDefault[index]);
}
$get("divAdd").innerHTML=sbadd.toString();
var arrMid=['k','e','y'];
Array.clear(arrMid);
//清除数组arrMid中的所有项
if(arrMid.length==0){
$get("divClear").innerHTML="NULL Value";
}
var strcontain="e";
if(Array.contains(arrDefault,strcontain)){
//判断数组arrDefault是否包含字符串strcontian5
$get("divContain").innerHTML="Key_Sky contains e";
}
Array.dequeue(arrDefault);
//移除数组arrDefault的第一项
var sbdequeue=new Sys.StringBuilder();
for(index=0;index<arrDefault.length;++index){
sbdequeue.append(arrDefault[index]);
}
$get("divDequeue").innerHTML=String.format("{0} dequeued 'k'",sbdequeue);
var result='';
function GetAllItem(item){
result+=item+this+',';
}
Array.forEach(arrDefault,GetAllItem,'$');
//遍历arrDefault并为每项加上指定的方法
$get("divForEach").innerHTML=result;
var indexof= Array.indexOf(arrDefault,'_');
//从开始搜索数组arrDefault中出现'_'的索引,如果不包含则为'-1',如果指定start项,返回的为
//从start项开始的索引
$get("divIndexOf").innerHTML=indexof;
Array.insert(arrDefault,0,'k');
//将'k'插入到数组arrDefault的第0项
var sbinsert=new Sys.StringBuilder();
for(index=0;index<arrDefault.length;++index){
sbinsert.append(arrDefault[index]);
}
$get("divInsert").innerHTML=String.format("{0} contains {1} again",sbinsert,'k');
var sbremoveitem=new Sys.StringBuilder();
Array.remove(arrDefault,'_');
//从数组arrDefault中移除_;
var result2='';
function GetRemovedItem(element){
result2+=element;
}
Array.forEach(arrDefault,GetRemovedItem);
$get("divRemoveItem").innerHTML=result2;
Array.removeAt(arrDefault,1);
//移除数组arrDefault的第一项
var result3='';
function GetRemovedIndex(element){
result3+=element;
}
Array.forEach(arrDefault,GetRemovedIndex);
$get("divRemoveIndex").innerHTML=result3;
</script>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Array Extensions </title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<div id="divAddRange">
</div>
<div id="divAdd">
</div>
<div id="divClear">
</div>
<div id="divContain">
</div>
<div id="divDequeue">
</div>
<div id="divForEach">
</div>
<div id="divIndexOf">
</div>
<div id="divInsert">
</div>
<div id="divRemoveItem">
</div>
<div id="divRemoveIndex">
</div>
<script type="text/javascript">
var arrDefault=['k','e','y','_','s','k','y'];
var arrChi=['H','e','l','l','o'];
Array.addRange(arrDefault,arrChi);
//将数组arrchi中的项依次添加到数组arrDefault的结尾
var sb=new Sys.StringBuilder();
for(index=0;index<arrDefault.length;++index){
sb.append(arrDefault[index]);
}
$get("divAddRange").innerHTML=sb.toString();
var str='!';
Array.add(arrDefault,str);
//将字符串str添加到数组arrDefault的末尾
var sbadd=new Sys.StringBuilder();
for(index=0;index<arrDefault.length;++index){
sbadd.append(arrDefault[index]);
}
$get("divAdd").innerHTML=sbadd.toString();
var arrMid=['k','e','y'];
Array.clear(arrMid);
//清除数组arrMid中的所有项
if(arrMid.length==0){
$get("divClear").innerHTML="NULL Value";
}
var strcontain="e";
if(Array.contains(arrDefault,strcontain)){
//判断数组arrDefault是否包含字符串strcontian5
$get("divContain").innerHTML="Key_Sky contains e";
}
Array.dequeue(arrDefault);
//移除数组arrDefault的第一项
var sbdequeue=new Sys.StringBuilder();
for(index=0;index<arrDefault.length;++index){
sbdequeue.append(arrDefault[index]);
}
$get("divDequeue").innerHTML=String.format("{0} dequeued 'k'",sbdequeue);
var result='';
function GetAllItem(item){
result+=item+this+',';
}
Array.forEach(arrDefault,GetAllItem,'$');
//遍历arrDefault并为每项加上指定的方法
$get("divForEach").innerHTML=result;
var indexof= Array.indexOf(arrDefault,'_');
//从开始搜索数组arrDefault中出现'_'的索引,如果不包含则为'-1',如果指定start项,返回的为
//从start项开始的索引
$get("divIndexOf").innerHTML=indexof;
Array.insert(arrDefault,0,'k');
//将'k'插入到数组arrDefault的第0项
var sbinsert=new Sys.StringBuilder();
for(index=0;index<arrDefault.length;++index){
sbinsert.append(arrDefault[index]);
}
$get("divInsert").innerHTML=String.format("{0} contains {1} again",sbinsert,'k');
var sbremoveitem=new Sys.StringBuilder();
Array.remove(arrDefault,'_');
//从数组arrDefault中移除_;
var result2='';
function GetRemovedItem(element){
result2+=element;
}
Array.forEach(arrDefault,GetRemovedItem);
$get("divRemoveItem").innerHTML=result2;
Array.removeAt(arrDefault,1);
//移除数组arrDefault的第一项
var result3='';
function GetRemovedIndex(element){
result3+=element;
}
Array.forEach(arrDefault,GetRemovedIndex);
$get("divRemoveIndex").innerHTML=result3;
</script>
</form>
</body>
</html>
Boolean&Date:
Code
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Boolean*Date Extensions</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<div id="divBoolean"></div>
<div id="divSimpleDate"></div>
<div id="divFullDate"></div>
<div id="divParseDate"></div>
<script type="text/javascript">
var boolthis=Boolean.parse("false");
//将true,false从字符串格式转化为boolean格式;
$get("divBoolean").innerHTML=boolthis;
var datethis=new Date();
var simpledate=datethis.format("d");
//转化为简单的日期所写格式
var fulldate=datethis.format("F");
//转化为完整时间日期
var parsedate="06/09/2009";
Date.parseInvariant(parsedate,"mm/dd/yyyy","MM-DD-YYYY","YYYY-MM-DD");
//以几种格式尝试转化parsedate,若无任一匹配则抛出异常
$get("divSimpleDate").innerHTML=simpledate;
$get("divFullDate").innerHTML=fulldate;
$get("divParseDate").innerHTML=parsedate;
</script>
</form>
</body>
</html>
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Boolean*Date Extensions</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<div id="divBoolean"></div>
<div id="divSimpleDate"></div>
<div id="divFullDate"></div>
<div id="divParseDate"></div>
<script type="text/javascript">
var boolthis=Boolean.parse("false");
//将true,false从字符串格式转化为boolean格式;
$get("divBoolean").innerHTML=boolthis;
var datethis=new Date();
var simpledate=datethis.format("d");
//转化为简单的日期所写格式
var fulldate=datethis.format("F");
//转化为完整时间日期
var parsedate="06/09/2009";
Date.parseInvariant(parsedate,"mm/dd/yyyy","MM-DD-YYYY","YYYY-MM-DD");
//以几种格式尝试转化parsedate,若无任一匹配则抛出异常
$get("divSimpleDate").innerHTML=simpledate;
$get("divFullDate").innerHTML=fulldate;
$get("divParseDate").innerHTML=parsedate;
</script>
</form>
</body>
</html>
Object&String:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ObjectStringExtensions.aspx.cs" Inherits="ObjectStringExtensions" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ObjectStringExtensions</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<div id="divStart"></div>
<div id="divEnd"></div>
<div id="divTrim"></div>
<div id="divFormat"></div>
<script type="text/javascript">
//Object.getType(instance) 返回对象的类型
//Object.getTypeName(instance) 返回一个表示对象在运行时的完整限定类型的字符串
var stringthis="key_sky ";
var start=stringthis.startsWith("key");
//判断字符串stringthis是否以key开始;
$get("divStart").innerHTML=start;
var end=stringthis.endsWith("key");
//判断字符串stringthis是否以key结束
$get("divEnd").innerHTML=end;
var newstring=stringthis.trim();
//清除字符串的首尾空格,与C#一样,还有trimStart()清除字符串开始的空格和trimEnd()清除字符串结束的空格
$get("divTrim").innerHTML=newstring;
var hello="hello";
var formatstring=String.format("{0},{1}!",hello,newstring);
//格式化字符串,格式中的大括号里的部分依次以后面的参数代替
$get("divFormat").innerHTML=formatstring;
</script>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ObjectStringExtensions.aspx.cs" Inherits="ObjectStringExtensions" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ObjectStringExtensions</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<div id="divStart"></div>
<div id="divEnd"></div>
<div id="divTrim"></div>
<div id="divFormat"></div>
<script type="text/javascript">
//Object.getType(instance) 返回对象的类型
//Object.getTypeName(instance) 返回一个表示对象在运行时的完整限定类型的字符串
var stringthis="key_sky ";
var start=stringthis.startsWith("key");
//判断字符串stringthis是否以key开始;
$get("divStart").innerHTML=start;
var end=stringthis.endsWith("key");
//判断字符串stringthis是否以key结束
$get("divEnd").innerHTML=end;
var newstring=stringthis.trim();
//清除字符串的首尾空格,与C#一样,还有trimStart()清除字符串开始的空格和trimEnd()清除字符串结束的空格
$get("divTrim").innerHTML=newstring;
var hello="hello";
var formatstring=String.format("{0},{1}!",hello,newstring);
//格式化字符串,格式中的大括号里的部分依次以后面的参数代替
$get("divFormat").innerHTML=formatstring;
</script>
</form>
</body>
</html>
面向对象:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ObjectOriented.aspx.cs" Inherits="ObjectOriented" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ObjectOriented</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<script type="text/javascript">
Type.registerNamespace("key_sky");
//声明命名空间为key_sky;
key_sky.HandBook=function (name){
return this._name=name;
}
//声明类HandBook;
key_sky.HandBook.prototype={
get_Name:function(){
return this._name;
},
set_Name:function(){
return this._name;
},
old: function(){
Sys.Debug.trace("HandBook is old");
}
}
//js中prototype机制注册类的方法
key_sky.HandBook.registerClass("key_sky.HandBook");
//registerClass()方法注册Old类
key_sky.Bracelent=function(name){
key_sky.Bracelent.initializeBase(this,[name]);
//调用父类构造函数,构造父类所必须的参数传递给父类
}
key_sky.Bracelent.registerClass("key_sky.Bracelent",key_sky.HandBook);
var handBook=new key_sky.HandBook("Nike");
Sys.Debug.trace(handBook.get_Name());
handBook.old();
</script>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ObjectOriented.aspx.cs" Inherits="ObjectOriented" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ObjectOriented</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<script type="text/javascript">
Type.registerNamespace("key_sky");
//声明命名空间为key_sky;
key_sky.HandBook=function (name){
return this._name=name;
}
//声明类HandBook;
key_sky.HandBook.prototype={
get_Name:function(){
return this._name;
},
set_Name:function(){
return this._name;
},
old: function(){
Sys.Debug.trace("HandBook is old");
}
}
//js中prototype机制注册类的方法
key_sky.HandBook.registerClass("key_sky.HandBook");
//registerClass()方法注册Old类
key_sky.Bracelent=function(name){
key_sky.Bracelent.initializeBase(this,[name]);
//调用父类构造函数,构造父类所必须的参数传递给父类
}
key_sky.Bracelent.registerClass("key_sky.Bracelent",key_sky.HandBook);
var handBook=new key_sky.HandBook("Nike");
Sys.Debug.trace(handBook.get_Name());
handBook.old();
</script>
</form>
</body>
</html>
调试输出: