Ad
2: 2016-03-18 (金) 16:04:21 njf ソース バックアップ No.2 を復元して編集 現: 2016-03-19 (土) 11:54:06 njf ソース 編集
Line 7: Line 7:
     return x + y;      return x + y;
 }  }
 + 
 console.log(testFunc(2,4)); // => 6  console.log(testFunc(2,4)); // => 6
Line 16: Line 16:
     return x + y;      return x + y;
 }  }
 + 
 testFunc = function(x:number,y:number,z:number):number{ //エラー  testFunc = function(x:number,y:number,z:number):number{ //エラー
     return x + y + z;      return x + y + z;
Line 28: Line 28:
     return x + y;      return x + y;
 }  }
 + 
 var test2Func:(x:number,y:number,z:number)=>number;  var test2Func:(x:number,y:number,z:number)=>number;
 + 
 test2Func = function(x:number,y:number,z:number):number{  test2Func = function(x:number,y:number,z:number):number{
     return x + y + z;      return x + y + z;
Line 44: Line 44:
*オプション引数とデフォルト引数 [#cf68938f] *オプション引数とデフォルト引数 [#cf68938f]
-関数の引数に「?」をつけると引数の数を変えられるオプション引数にできる。+関数の引数に「?」をつけると引数を省略できるオプション引数にできる。
 function testFunc(x:number,y?:number){  function testFunc(x:number,y?:number){
Line 52: Line 52:
     return x;      return x;
 }  }
 + 
 console.log(testFunc(1)); // => 1  console.log(testFunc(1)); // => 1
 console.log(testFunc(1,2)); // => 3  console.log(testFunc(1,2)); // => 3
Line 61: Line 61:
     return x + y;      return x + y;
 }  }
 + 
 console.log(testFunc(1)); // => 11  console.log(testFunc(1)); // => 11
 console.log(testFunc(1,2)); // => 3  console.log(testFunc(1,2)); // => 3
Line 72: Line 72:
     return a.join(",");      return a.join(",");
 }  }
 + 
 console.log(testFunc("one","two","three","four")) // => one,two,three,four  console.log(testFunc("one","two","three","four")) // => one,two,three,four
Line 91: Line 91:
   }    }
  };   };
 + 
 var w = wrapper.whatisx();  var w = wrapper.whatisx();
 + 
 console.log(w()); // => undefined  console.log(w()); // => undefined
Line 106: Line 106:
   }    }
 };  };
 + 
 var w = wrapper.whatisx();  var w = wrapper.whatisx();
 + 
 console.log(w()); // => 2  console.log(w()); // => 2
Line 115: Line 115:
*オーバーライド [#da6d0324] *オーバーライド [#da6d0324]
-関数の定義を並べるとオーバーライドも可能。+関数の定義を並べると変数や戻り値の型や個数を変えるオーバーライドも可能。
 function testFunc(x:number):number;  function testFunc(x:number):number;
 function testFunc(x:string):string;  function testFunc(x:string):string;
- + function testFunc(x:number,y:number):number; 
- function testFunc(x:any):any{+  
 + function testFunc(x:any,y:number = 1):any{
     if(typeof x == "number"){      if(typeof x == "number"){
-         return x;+         return x + y;
     }else if(typeof x == "string"){      }else if(typeof x == "string"){
         return "string:" + x;          return "string:" + x;
     }      }
 }  }
- +  
- console.log(testFunc(2)); // => 2+ console.log(testFunc(2)); // =>
 + console.log(testFunc(2,4)); // => 6
 console.log(testFunc("test")); // => "string:test"  console.log(testFunc("test")); // => "string:test"
 testFunc(true); // => エラー  testFunc(true); // => エラー


トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード印刷に適した表示   ページ新規作成 全ページ一覧 単語検索 最新ページの一覧   ヘルプ   最新ページのRSS 1.0 最新ページのRSS 2.0 最新ページのRSS Atom Powered by xpWiki
Counter: 2353, today: 1, yesterday: 0
MenuBar
広告

ログイン

ユーザー名:


パスワード:





パスワード紛失

Portuguese | English | German | Greek | Japanese | Korean | Russian | T-Chinese top
NJF