天天看點

Javascript複習 及 面向對象寫法

ExtJs??椋?缃?瀛???葷?

1.?????? JavaScript?版??绫誨????

杞???負Number锛??規??parseInt()?ヨ漿????

杞???tring锛?浠諱?绫誨????瀛?绗?覆?充負瀛?绗?覆??

杞???oolean锛?true,false,??????0?闆??涓?rue,????涓?alse

Undefind? / null ?存?ヨ漿??負false

?

2.?????? JavaScript?版??涓???

??烘???版??绫誨?????ㄥ?間???锛?

?

3.?????? JavaScript璇?娉?? ?芥?頒?浜?浠?

?芥?闆??浜?浠堕??甯告??涓?绉?缁?瀹??崇郴锛???杩?浜?浠惰??ㄥ?芥?般??濡???缁?瀹?澶?涓??芥?幫?涓??寸?ㄥ???烽??寮???

l?????? 甯哥?ㄤ?浠?

l?????? Onload

l?????? OnClick

l?????? MouseOver 榧???绉誨?ㄥ?版??瀹?????涓?

l?????? MouseOut 榧???绂誨???瀹?????

l?????? Focus 褰???绱??峰?????

l?????? Blur 褰???绱?澶卞?葷????

l?????? Change 1.杈??ュ??瀹瑰?????瑰??锛?2.select??椤瑰?????瑰??

l?????? Onkeyup ????妗?浠跺脊璧?

l?????? 甯哥?ㄤ?浠?

l?????? Onload

l?????? OnClick

l?????? MouseOver 榧???绉誨?ㄥ?版??瀹?????涓?

l?????? MouseOut 榧???绂誨???瀹?????

l?????? Focus 褰???绱??峰?????

l?????? Blur 褰???绱?澶卞?葷????

l?????? Change 1.杈??ュ??瀹瑰?????瑰??锛?2.select??椤瑰?????瑰??

l?????? Onkeyup ????妗?浠跺脊璧?

?

4.?????? JavaScript?㈠??瀵矽薄缂?绋?

JavaScript瀹?涔?灞??у???規?

?規?涓?锛?

function Test(){
      //javascript瀹?涔?灞???       Test.prototype.name="zsw";
       //javascript瀹?涔??規?
       Test.prototype.say = function(){
              alert(this.name + " love hy!");
       };
}
 
function a(){
       new Test().say(); //javascript璋??ㄦ?規?
}      

?

? ?規?浜?锛?

function boy(){
       //js??杩?this瀹?涔?灞???       this.name = "zsw";
       this.age = 21;
       //js??杩?this瀹?涔??規?
       this.say = function(){
              alert(this.name + " love hy!");
       }
};
 
function b(){
       var _boy = new boy();
       alert(_boy.name);
       alert(_boy.age+"");
       _boy.say();
};      

?葷?:

瀹?涔?灞??э??規?

1.浣跨??rototype??寤?

2.浣跨??his??寤?

?

?

5.?????? Json???瑰???寤哄?矽薄

var json = {name:'zsw',age:20};
       alert(json.name);      

?

function showName(o){
       alert(o.name);
       alert(o["name"]);
}
 
function showObject(){
       var json = "{name:'zsw',age:20}";
       
       
       showName(eval锛?"("+json+")"));
}      

?

Json瀵矽薄????浠ヤ?????

????锛?JSON??浠ュ?ㄥ??绗?覆??瀵矽薄?磋漿????

??浠ラ??杩?JavaScript??eval锛?)?規?灏?瀛?绗?覆杞??㈡??瀵矽薄??

?

6锛??ㄦ??娣誨??灞??у???規?

function test1(){};
function runTest1(){
       var t1 = new test1();
       t1.name = "zsw";
       t1.age = 21;
       t1.say = function(){
              alert("Hello World!!!");
       };
       alert(t1.name);
       t1.say();  
};      

?

7.?ㄥ??缃?瀵矽薄娣誨??灞??у???規?

?

?

8.Javascript?㈠??瀵矽薄涓???缁ф??

function Person(){
       Person.prototype.name = "zsw";
       Person.prototype.age = 21;
       Person.prototype.say = function(){
              alert("Javascript ?㈠??瀵矽薄??缁ф??);
       };
}
 
function Student(){
       Student.prototype.schoolName = "bdqn";
}
 
function TestStu(){
       Student.prototype = new Person();
       var s = new Student();
       alert(s.schoolName);
       s.say();
}      

?

9锛?瀹?涔?绉???灞???

function Person(){
       Person.prototype.name = "zsw";
       Person.prototype.age = 21;
       Person.prototype.say = function(){
              alert("Javascript ?㈠??瀵矽薄??缁ф??);
       };
}
 
function Student(){
       Student.prototype.schoolName = "bdqn";
       //瀹?涔?绉???灞???       var gfName = "hy";
}
 
function TestStu(){
       Student.prototype = new Person();
       var s = new Student();
       alert(s.schoolName);
       s.say();
       
       alert(s.gfName); //涓???瑙?锛???涓鴻?塊????绉???灞??? 
}      

?

10.瀹?涔?????灞???

function Person(){
       Person.prototype.name = "zsw";
       Person.prototype.age = 21;
       Person.prototype.say = function(){
              alert("Javascript ?㈠??瀵矽薄??缁ф??);
       };
}
 
function Student(){
       Student.prototype.schoolName = "bdqn";
       //瀹?涔?绉???灞???       var gfName = "hy";
       Student.cityName = "娣卞??;
}
 
function TestStu(){
       Student.prototype = new Person();
       var s = new Student();
       alert(s.schoolName);
       s.say();
       
       alert(s.gfName); //涓???瑙?锛???涓鴻?塊????绉???灞???       
       //璁塊??????灞???       alert(Student.cityName);
}      

?

?

11.Json瀵矽薄

瀹?涔?Json瀵瑰?矽薄锛??ㄥ?矽薄涓?娣誨??灞??э?

function jsonTest(){
       var jsonObj = {
              name:'?ㄥ?姝?,
              sex:'??,
              age:21
       };
       
       alert(jsonObj.name);
}      

Json娣誨???規?锛?

function jsonTest(){
       var jsonObj = {
              name:'?ㄥ?姝?,
              sex:'??,
              age:21,
              say:function(){
                     alert("Json瀵矽薄涓?娣誨???規?!");
              }
       };
       
       alert(jsonObj.name);
       jsonObj.say();
}      

Json娣誨??瀛?瀵矽薄锛?

function jsonTest(){
       var jsonObj = {
              name:'?ㄥ?姝?,
              sex:'??,
              age:21,
              say:function(){
                     alert("Json瀵矽薄涓?娣誨???規?!");
              }
       };
       
       alert(jsonObj.name);
       jsonObj.say();
       
       
       var jsonArray = ["A","B","C"];
       alert(jsonArray[0]);
}      

?ㄦ?ゅ?涔?涓?涓??????規?

function test(){
       test.prototype.name="huangyi";
       test.cityName="娣卞??;
}
function T(){
       alert(test.cityName); //灏?杈???ndefined
}      

浠ヤ?璋???est.cityName蹇?椤誨????test瀹?渚???涓?涓?锛?

function test(){
       test.prototype.name="huangyi";
       test.cityName="娣卞??;
}
function T(){
       new test();
       alert(test.cityName);
}      

?葷?锛?

Javascript?㈠??瀵矽薄???????㈠??瀵矽薄缂?绋?璇?瑷?涓??鳳??芥????杩?瀹?渚??????瑰?杩?琛?璁塊??锛?涓や釜瀵矽薄?寸????????????涓?浼?琚?骞叉?般??

JSON涓?锛?灞??у??浠ユ???規?锛????沖??浠ユ??涓?涓?瀛?瀵矽薄??

浣跨?ㄩ????????锛???瑕?瀹?渚???涓?涓?function锛???璇?娴?瑙??ㄥ??涓????芥?幫?????瀵矽薄??

?

?

12.?規?瑕???

function test(){
       test.prototype.name = "zsw";
       test.prototype.age = 21;
       test.prototype.say=function(){
              alert("Hello World!");
       }
}
 
function T(){
       var t = new test();
       t.say = function(){
              alert("浣?濂?");
       }
       t.say();
}      

?

13.????window.alert();??绀烘?

<script type="text/javascript">
function run(){
       window.alert = function(s){
              var o = document.getElementById("Layer1");
              o.innerHTML = "<input name='button' type='button' 慰nclick='closeWin()' value='?抽??' />"+s;
              o.style.display = "block";
       }
       
       window.show = function(s){
              alert(s);
       }
       
       show('zsw love hy锛?锛?');
}
 
function closeWin(){
       var o = document.getElementById("Layer1");
       o.style.display = "none";
}
</script>
<style type="text/css">
<!--
#Layer1 {
       position:absolute;
       left:341px;
       top:168px;
       width:296px;
       height:142px;
       z-index:1;
       color:#FFFFFF;
       display:none;
       background-color:#00CC99;
}
-->
</style>
</head>
 
<body>
<div id="Layer1">
  
</div>
<input type="button" value="?瑰?? 慰nclick="run()" />
</body>      

?

琛ュ??锛?javascript澶??剁戶?挎?

<mce:script type="text/javascript"><!--
  function dw(s){
    alert(s);
  }
  
  Function.prototype.extend_p = function(obj){
    for(var i in obj){
      this.prototype[i] = obj[i];
    }
  }
  
  Person = function(){
    this.name = "accp";
    this.setName = function(obj){
      this.name = obj;
    }
    this.getName = function(){
      return this.name;
    }
  }
  
  
  Student = function(){
  
  }
  
  Student.extend_p(new Person());
  
  var s = new Student();
  s.setName('榛?缈?');
  dw(s.getName());
  
// --></mce:script>      

?

<!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>
<mce:script type="text/javascript"><!--
  function ds(s){
    alert(s);
  }
  
  Person = function(){
    this.name = "hy";
    this.setName = function(obj){
      this.name = obj;
    }
    this.getName = function(){
      return this.name;
    }
  }
  
  Student = function(){
    this.stuNo = "0001";
  }
  
  Student.prototype = new Person();
  
  var stu = new Student();
  
  stu.setName('榛?缈?');
  alert("??瀛?锛?"+stu.getName());
  alert("缂??鳳?"+stu.stuNo);
// --></mce:script>
</head>
<body>
</body>
</html>      

繼續閱讀