JavaScript語法學習筆記

我搞j2ee屬於半路出傢,沒有系統學習過,大多功能是會用,但講不出道道,不成體系.

在jsp文件中常使用jquery,對javascript屬於現學現用.

看到的朋友有任何意見和建議請多提

—————————————————————————————————————————–

共包含 日期格式, url提取及分解,對象 三部分內容


<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript語法</title>
      
      
</head>
<script type="text/javascript">
<!–  
var thisDate = new Date();
/*日期格式*/
document.writeln(thisDate.toString());
document.writeln("<br/>");
document.write(thisDate.getFullYear() + "-" + thisDate.getMonth() + "-" + thisDate.getDay());
document.writeln(" ");
document.writeln(thisDate.getHours() + ":" + thisDate.getMinutes() + ":" + thisDate.getSeconds() + "." + thisDate.getMilliseconds() );
/*URL分解*/
document.write("<hr/>");
//設定一個帶有參數的URL  如:    file:///E:/Documents%20and%20Settings/simon/%E6%A1%8C%E9%9D%A2/Untitled-1.html?aa=11&bb=cc
var url = document.URL.split("?");
var param = url[1].split("&");
for(i = 0; i < param.length; i++){
    var val = param[i].split("=");
    document.write(val[0] + "=" + val[1]);
    document.write("<br/>");
}     
      
//打開新document對象
document.write("<hr/>");
document.write("<input type='button' value='New document' onclick='newDocument()'/>");
function newDocument(){
    document.open();
    document.write("New Document");
    window.setTimeout(window.close(), "3000");
}     
      
//網頁加載進度窗口, 頁面沒有內容時刷的太快, 可以通過onload控制.
//var placeHolder2 = document.open('Untitled-2.html','test','width=300,height=300');
      
      
//Javascript對象
/*對象
    屬性可以在對象內定義,如name,age;也可以在對象外定義,如addr
    方法分兩種方式定義,見say和ageAdd的定義
 */   
function Person(){
    this.name = 'aa';
    this.age = 12;
    this.say = sayPerson;
    this.ageAdd=function(){
        this.age++;
    } 
}     
function sayPerson(){
    alert(this.name + "," + this.age + "," + this.addr);   
}     
function newP(){
    var p = new Person();
    p.addr = "abcd";
    p.say();
    p.ageAdd();
    p.say();
}     
      
//對象2 簡單的自定義對象 內部函數
function cHuman(){
    function sayHuman(attr){
        return human[attr];
    } 
    var human = new Object();
    human.userName = "abc";
    human.userAge = 12;
    alert(sayHuman("userAge"));
    alert(sayHuman("userName"));
}     
      
//對象3
function Simon(){this.userName; this.userAge;}
//Simon.prototype.userName = "";
Simon.prototype.setUserName = function(userName){this.userName = userName;}
Simon.prototype.getUserName = function(){return this.userName;}
Simon.prototype.setUserAge = function(userAge){this.userAge = userAge;}
Simon.prototype.getUserAge = function(){return this.userAge;}
function cSimon(){
    var simon = new Simon();
    simon.setUserName("Simon hahah ");
    simon.setUserAge(22);
    alert(simon.getUserName() + "  " + simon.getUserAge());
    with(simon){//with的用法
        alert(getUserName() + ", " + getUserAge());
    } 
    var p, str = "";
    for(p in simon){
        str +=p + "\n";
    } 
    alert(str);
}     
      
//對象4
function cObj(){
    function Obj(){this.userName = "aa"; this.userAge = 55;}
    var obj = new Obj();
    var p, str = "";
    for(p in obj){//for的用法
        str +=p + "=" + obj[p] + "\n";
    } 
    alert(str);
}     
      
//對象5 數組對象
function MyArr(){
    this.length = arguments.length;
    for(i = 0; i < this.length; i++){
        this[i] = arguments[i];
    } 
}     
function cMyArr(){
    var myArr = new MyArr("abc", 12, '你好', 3.5, true);
    var str = "";
    for(i = 0; i < myArr.length; i++){
        str += myArr[i] + ", ";
    } 
    alert(str);
      
    var p;
    str = "";
    for(p in myArr){
        str += p + "=" + myArr[p] + "\n";
    } 
    alert(str);
}     
      
//對象6 數組對象
function MyArr2(size){
    this.length = size;
    for(i = 0; i < this.length; i++){
        this[i] = "";
    }
}     
function cMyArr2(){
    var myArr2 = new MyArr2(6);

    myArr2[0] = "aa";
    myArr2[1] = 3;
    myArr2[2] = 'dfadfa';
    myArr2[3] = true;
    myArr2[5] = 0.4;       
    var p, str = "";
    for(p in myArr2){//for的用法
        str +=p + "=" + myArr2[p] + "\n";
    }
    alert(str);
}      
      
      
      
//–> 
</script>
<body onload="placeHolder.close();">
<button value="new person" onclick="newP()" >new person</button>
<button value="new person" onclick="cHuman()" >new Human</button>
<button value="new person" onclick="cSimon()" >new Simon</button>
<button value="new person" onclick="cObj()" >new Obj</button>
<button value="new person" onclick="cMyArr()" >new MyArr</button>
<button value="new person" onclick="cMyArr2()" >new MyArr2</button>
</body>
</html>

發佈留言