javascript的一些常用例子

<textarea cols="50" rows="15" name="code" class="javascript"> alert(new Date().toLocaleDateString()); alert(new Date().toLocaleTimeString()); //创建对象的函数 function Person(name, age) { this.Name = name; this.Age = age; this.SayHello = function () { return this.Name + "的年龄是:" + this.Age }; } var p1 = new Person("ac", 32); alert(p1.SayHello()); //匿名函数 var f1 = function (i1, i2) { return i1 + i2 }; alert(f1(2,3)); var j; if (!j) { alert(j+"非空的j"); } //计算1-100之间的数的和 var sum = 0; for (var i = 0; i &lt;= 100; i++) { sum += i; } alert(sum); //将一个数组的元素以|分隔形式输出 function myJoin(arr) { if (arr.length &lt;= 0) { return ""; } var s = arr[0]; for (var i = 1; i &lt; arr.length; i++) { s = s + "|" + arr[i]; } return s; } var arr1 = new Array(); arr1[0] = "tom"; arr1[1] = "ac"; arr1[2] = "ts"; //alert(myJoin(arr1)); alert(arr1.join("|"));//自带的函数 //将一个数组的元素反转 function myReverse(arr) { if (arr.length &lt;= 0) { return ""; } for (var i = 0; i &lt; arr.length/2; i++) { var temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } return arr; } var arr1 = new Array(); arr1[0] = "tom"; arr1[1] = "ac"; arr1[2] = "ts"; //alert(myReverse(arr1)); alert(arr1.reverse());//自带的函数 var p1 = new Object(); p1.Name = "ac"; p1.Age = 34; p1.SayHello = function () { alert("hello"); }; p1.SayHello(); for (var k in p1) { alert(k); } var array = new Array(); array[0] = "tom"; array[1] = "jerry"; var array1 = [1, 2, 3]; var array2 = ["tom", "jerry"]; var array3 = new Array("red", "blue"); var array4 = { "tom": 23, "ac": 31 }; var arr1 = [1, 2, 3]; //数组的简化方式 var arr2 = { "tom": 39, "jim": 68 };//简化的数组"tom"为key alert(arr2["tom"]); var dict = new Array(); dict["人"] = "ren"; dict["口"] = "kou"; dict["手"] = "shou"; //alert(dict["人"]); for (var k in dict) { alert(k); }</textarea>

你可能感兴趣的:(JavaScript,Date,function,object)