<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div id="info">
</div>
<script language="javascript" type="text/javascript">
function display(text) {
document.getElementById("info").innerHTML += (text + "<br/>");
}
function useAsDictionary() {
var obj=new Object();
obj["name"]="xiaoyaojian";//Key="name",Value="xiaoyaojian"
obj.salary=9000;//这是另外一种设置值的方式
display("Name:"+obj.name);
display("Salary:"+obj["salary"]);
display("____________________");
}
function usrForInStatement() {
var dictionary = new Object();
for (var i = 0; i < 10; i++) {
var key = "key_" + i;
dictionary[key] = Math.random();
}
delete dictionary["key_6"]; //删除Key为“key_6”的元素,不可只用dictionary["key_6"]=null这种方式
for (var key in dictionary) { //使用for-in遍历Object
display("Key:"+key+",value="+dictionary[key]);
}
}
useAsDictionary();
usrForInStatement()
</script>
</body>
</html>
new Array();//空数组
new Array();//长度为3的数组,当然,添加多于三个的元素也是可以的,只是初始长度为3
new Array(1,"Xiaoyaojian");//构造两个元素的数组
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div id="info"></div>
<script language="javascript" type="text/javascript">
function display(text) {
document.getElementById("info").innerHTML+=(text+"<br/>");
}
Array.prototype.display = function() {
window.display("a:"+this);
}
var a = new Array(1, 2, 3, 4, 5);
display("Array initialized");
a.display();
display("a.push(6,7)");
a.push(6, 7);
a.display();
display("a.shift():" + a.shift());
a.display();
display("a.unshift(8)");
a.unshift(8);
a.display();
display("a.pop()" + a.pop());
a.display();
display("a.concat(9,10)" + a.concat(9, 10));
a.display();
display("'['+a.join('][')+']':" + '[' + a.join('][') + ']');
a.display();
display("a.slice(2,5):" + a.slice(2, 5));
display("a.slice(2,-2)" + a.slice(2, -2));
a.display();
display("a.splice(3,2)");
a.splice(3, 2);
a.display();
display("a.splice(2,0,7,8)");
a.splice(2, 0, 7, 8);
a.display();
a.display("a.splice(3,1,9,10)");
a.splice(3, 1, 9, 10);
a.display();
display("a.reverse()");
a.reverse();
a.display();
display("a.sort()");
a.sort();
a.display();
display("a.sort(function(x,y){return y-x})");
a.sort(function(x, y) { return y - x });
a.display();
</script>
</body>
</html>
这样我们看结果,就能很清楚的看明白这些函数的作用和用法
创建一个aspx的页面,因为我们要使用Microsoft AJAX Library为我们提供的对于Array的扩展,所以需要在页面中添加一个ScriptManager
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
function method(elt, index, array) {//etc:枚举的当前元素,index:当前下标,array:当前被遍历的array
this.result += "[" + index + ". " + elt + "]";
}
var items = "i am xiaoyaojian".split(" ");
var obj = { result: "" };
Array.forEach(items,method,obj);
alert(obj.result);
</script>
</form>
</body>
</html>
这样我们就能清楚的看到Array.forEach这个方法的用户啦
创建一个html页面
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script language="javascript" type="text/javascript">
try {
throw new Error("一个示例错误");
alert("s");
}
catch (e) {
var errorMsg = ("Message:" + e.message + "\n");
if (!e.stack) {//判断是否为IE,IE没有Error.stack
errorMsg += "Description:" + e.description + "\n";
errorMsg += "Number:" + e.number;
}
else {
errorMsg += "Line Number:" + e.lineNumber + "\n";
errorMsg += "File Name:" + e.fileName + "\n\n";
errorMsg += "Stack Trace:\n" + e.stack;
}
alert(errorMsg);
}
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
Please view the page in FireFox
<script language="javascript" type="text/javascript">
function getNiceError() {
var e = Error.creat("Error Message", { customMessage: "Custom Message" });
e.popStackFrame();
return e;
}
try {
throw getNiceError();
}
catch (e) {
var errorMsg = ("Message:" + e.message + "\n");
errorMsg += "Line Number:" + e.lineNumber + "\n";
errorMsg += "File Name:" + e.fileName + "\n\n";
errorMsg += "Stack Trace:\n" + e.stack;
alert(errorMsg);
}
</script>
</form>
</body>
</html>
在火狐中运行这个页面的时候,我们就可以看到更多的有用的错误信息
定义一个html页面
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div id="info"></div>
<script language="javascript" type="text/javascript">
function display(text) {
document.getElementById("info").innerHTML += text + "<br/>";
}
function aMethod() {
var result = this.toString();
for (var i = 0; i < arguments.length; i++) {
result += ", " + arguments[i];
}
return result;
}
var a = new String("i am string A");
var b = new String("i am string B");
a.aMethod = b.aMethod = aMethod;
display("aMethod():" + aMethod());
display("a.aMethod():" + a.aMethod());
display("b.aMethod():" + b.aMethod());
display("a.aMethod.call(b,1,2,3)" + a.aMethod.call(b, 1, 2, 3));
display("a.aMethod.apply(b,[1,2,3])" + a.aMethod.call(b,[ 1, 2, 3]));
</script>
</body>
</html>
我们看到,我们直接调用aMethod,出现的是object,实际上它的this当前指向window,而a.aMethod,this指向的是a对象,这里也能明显的看到call和apply的用法
创建一个aspx页面,添加一个ScriptManager
页面中添加如下代码
<asp:ScriptManager ID="s" runat="server" />
<input type="button" value="Click Me" id="btn" />
<script language="javascript" type="text/javascript">
var obj =
{
text: "xiaoyaojian",
onClick: function(e) {
alert(this.text);
}
}
obj.onClick();
</script>
这样,执行页面,就弹出一个“xiaoyaojian”
删除obj.onClick();添加$addHandler($get("btn"), "click", obj.onClick);,我们使用Microsoft AJAX Library提供的这种扩浏览器的方式,给按钮添加一个事件
点击按钮,将会弹出一个undefined,这里其实this指定的是当前的window对象,我们需要依旧把this指向obj,就需要这么做
var onClickDelegate = Function.createDelegate(obj, obj.onClick);
$addHandler($get("btn"), "click",onClickDelegate);
这样就解决了上面的问题了
我们修改上面的obj
var obj =
{
text: "xiaoyaojian",
onClick: function(e,args) {
alert(this.text+"\n"+args);
}
}
这样,当我们再次点击按钮,就会弹出一个“xiaoyajian undifined”,因为这时只传入了一个参数
这样,我们就需要使用Funcation.creatCallback(method,context)这个方法,
改变onClickDelegate的定义
var onClickDelegate = Function.createCallback(Function.createDelegate(obj, obj.onClick),"xiaobai");
这时,我们传入了一个额外的参数给obj,这样,我们就得到了想要的效果
创建一个html页面
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div id="info"></div>
<script language="javascript" type="text/javascript">
function display(text) {
document.getElementById("info").innerHTML += text + "<br/>";
}
display("Max Value" + Number.MAX_VALUE);
display("Min Value" + Number.MIN_VALUE);
display("____________________________");
display("Postive infinity=" + Number.POSITIVE_INFINITY);
display("Negative infinity=" + Number.NEGATIVE_INFINITY);
display("____________________________");
display("1/0=" + (1 / 0));
display("Max Value*10=" + Number.MAX_VALUE * 10);
display("Max Value+10=" + (Number.MAX_VALUE + 10));
display("Infinity/10=" + Number.POSITIVE_INFINITY / 10);
display("______________________________");
display("NaN=" + Number.NaN);
display("parseInt('ABC')=" + parseInt('abc'));
display("3-'ABC'=" + (3 - 'ABC'));
display("(NaN==NaN)=" + (NaN == NaN));
display("(NaN!=NaN)=" + (NaN != NaN));
display("isNaN(NaN)="+isNaN(NaN));
</script>
</body>
</html>
通过这个示例的结果,我们就难很清晰的理解这些原生类型啦
创建一个aspx页面
<asp:ScriptManager ID="s" runat="server" EnableScriptGlobalization="true"></asp:ScriptManager>
<div id="info"></div>
<script language="javascript" type="text/javascript">
function display(text) {
document.getElementById("info").innerHTML += text + "<br/>";
}
var num = 12345678.12345678;
display("num.format('p')=" + num.format("p"));
display("num.format('d')=" + num.format("d"));
display("num.format('c')=" + num.format("c"));
display("num.format('n')=" + num.format("n"));
display("_________________________________");
display("num.localeFormat('p')=" + num.localeFormat("p"));
display("num.localeFormat('d')=" + num.localeFormat("d"));
display("num.localeFormat('c')=" + num.localeFormat("c"));
display("num.localeFormat('n')=" + num.localeFormat("n"));
</script>
打开网页,我们就可以对比的看到各个格式代码与对应的格式啦,打开网页源文件,我们也可以看到关于格式的JSON字符串,以in为我们现在已经设置了EnableScriptGlobalization="true"
创建一个aspx页面
添加如下代码
<asp:ScriptManager runat="server" ID="s" EnableScriptGlobalization="true" />
<div id="info">
</div>
<script language="javascript" type="text/javascript">
function display(text) {
document.getElementById("info").innerHTML += text + "<br/>";
}
var now = new Date();
display("now.localeFormat('d')=" + now.localeFormat('d'));
display("now.localeFormat('D')=" + now.localeFormat('D'));
display("now.localeFormat('t')=" + now.localeFormat('t'));
display("now.localeFormat('T')=" + now.localeFormat('T'));
display("now.localeFormat('F')=" + now.localeFormat('F'));
display("now.localeFormat('M')=" + now.localeFormat('M'));
display("now.localeFormat('Y')=" + now.localeFormat('Y'));
display("now.localeFormat('yyyy年,MM月,dd日,hhhh:mm:ss tt dddd')=" + now.localeFormat('yyyy年,MM月,dd日,hhhh:mm:ss tt dddd'));
var d = Date.parseLocale("11/9/8", "yy/M/d");
display(d.format("i"));
</script>
示例很简单,我就不多做解释了
创建一个aspx页面,添加如下代码
<asp:ScriptManager ID="s" runat="server" EnableScriptGlobalization="true" />
<div id="info">
</div>
<script language="javascript" type="text/javascript">
function display(text) {
document.getElementById("info").innerHTML += text + "<br/>";
}
display(String.format("Today is {0}.", new Date()));
display(String.localeFormat("今天是{0:dddd}",new Date()));
</script>
这时,我们改变IE里的区域设置,就可以看到localeFormat这里的区别