2: 2016-03-18 (金) 16:04:21 njf |
現: 2016-03-19 (土) 11:54:06 njf |
| return x + y; | | return x + y; |
| } | | } |
| + | |
| console.log(testFunc(2,4)); // => 6 | | console.log(testFunc(2,4)); // => 6 |
| | | |
| 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; |
| 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; |
| *オプション引数とデフォルト引数 [#cf68938f] | | *オプション引数とデフォルト引数 [#cf68938f] |
| | | |
- | 関数の引数に「?」をつけると引数の数を変えられるオプション引数にできる。 | + | 関数の引数に「?」をつけると引数を省略できるオプション引数にできる。 |
| | | |
| function testFunc(x:number,y?:number){ | | function testFunc(x:number,y?:number){ |
| 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 |
| 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 |
| 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 |
| | | |
| } | | } |
| }; | | }; |
| + | |
| var w = wrapper.whatisx(); | | var w = wrapper.whatisx(); |
| + | |
| console.log(w()); // => undefined | | console.log(w()); // => undefined |
| | | |
| } | | } |
| }; | | }; |
| + | |
| var w = wrapper.whatisx(); | | var w = wrapper.whatisx(); |
| + | |
| console.log(w()); // => 2 | | console.log(w()); // => 2 |
| | | |
| *オーバーライド [#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)); // => 3 |
| + | console.log(testFunc(2,4)); // => 6 |
| console.log(testFunc("test")); // => "string:test" | | console.log(testFunc("test")); // => "string:test" |
| testFunc(true); // => エラー | | testFunc(true); // => エラー |