Corsair

          My javaWorld
          數據加載中……
          關于學習javaScript基礎概念后寫的示例代碼
          < script >
          ?@1?函數定義
          function?method(){
          ????????????var?str="";
          ????????????for(var?i=1;i<arguments.length;i++){
          ????????????????????str+=arguments[i]
          ????????????????}
          ????????????alert(str);
          ????}
          method(1,2,3,4,5,6,7,8);
          ----------------

          @2?隨機函數
          var?method=new?Function("x","y","var?sum=0;sum=x+y;return?sum;");
          alert(method(1,2));
          ----------------

          @3?對象定義
          function?obj(){}
          var?obj1=new?obj();
          obj1.name="corsiar";
          obj1.age=22;
          function?method(x,y){
          ????????????return?x+y;
          ????}
          obj1.add=method;
          alert(obj1["name"]);
          alert(obj1.age);
          alert(obj1.add(1,2));
          ----------------

          @4?對象的定義2
          function?hero(name?,hp,mp){
          ????????????this.name=name;
          ????????????this.hp=hp;
          ????????????this.mp=mp;
          ????????????this.s1=show1;
          ????????????this.s2=show2;
          ????}
          function?show1(){
          ????????????alert(this.hp+100);
          ????}
          function?show2(){
          ????????????alert(this.mp+100);
          ????}
          var?corsair=new?hero("corsair",100,300);
          ????????alert(corsair.hp);
          ????????alert(corsair.mp);
          ????????corsair.s1();
          ????????corsair.s2();
          ?

          ?@5?關于對象中with(對象名){}和for(變量in對象名){}的用法;
          function?person(name,age,sex,tel){
          ????????????this.name=name;
          ????????????this.age=age;
          ????????????this.sex=sex;
          ????????????this.tel=tel;
          ????}
          function?show(){
          ????????????alert(this.name);
          ????????????alert(this.sex);
          ????????????alert(this.tel);
          ????}
          var?p1=new?person("corsair","22","boy","13778519215");
          with(p1){
          ????????????var?str="";
          ????????????str+=name+"?";
          ????????????str+=age+"?";
          ????????????str+=sex+"?";
          ????????????str+=tel+"?";
          ????}
          alert(str);
          var?tmp,temp="";
          for(tmp?in?p1){
          ????????temp+=p1;
          ????}
          alert(str);
          ?

          </script>

          posted on 2006-07-29 00:56 銀河海盜 閱讀(322) 評論(0)  編輯  收藏 所屬分類: WEB

          主站蜘蛛池模板: 江永县| 宜兰市| 邯郸县| 新民市| 尼勒克县| 清镇市| 长海县| 湖口县| 鄢陵县| 江北区| 资阳市| 祥云县| 永德县| 福泉市| 田东县| 巴东县| 内黄县| 绍兴县| 鞍山市| 共和县| 德江县| 平远县| 扎鲁特旗| 台中市| 南乐县| 南皮县| 留坝县| 桐柏县| 英吉沙县| 始兴县| 德惠市| 宜城市| 遵义县| 磴口县| 增城市| 桂林市| 大竹县| 申扎县| 千阳县| 丹凤县| 宁南县|