2: 2016-03-18 (金) 16:04:21 njf[4] [5] [6] | 現: 2016-03-19 (土) 11:54:06 njf[4] [7] [8] | ||
---|---|---|---|
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)); // => 3 |
+ | console.log(testFunc(2,4)); // => 6 | ||
console.log(testFunc("test")); // => "string:test" | console.log(testFunc("test")); // => "string:test" | ||
testFunc(true); // => エラー | testFunc(true); // => エラー |
(This host) = https://njf.jp