「JavaScript/Function」の版間の差分
削除された内容 追加された内容
JavaScript/標準ライブラリ/Function 12:05, 7 August 2015 UTC を転記 |
→Functionオブジェクト: +name プロパティ |
||
7 行
==== length ====
この関数が取る引数の数を返す。
<source lang="javascript">
function foo(a,b,c){ return; }
console.log( foo.length ); // 3が表示される。
</source>
<source lang="javascript">
function foo(a, b, c) {
if (a === void 0) a = 2;
if (b === void 0) b = 3;
if (c === void 0) c = 5;
return `a: ${a}, b: ${b}, c:${c}`;
}
console.log( foo() ); // a: 2, b: 3, c:5
console.log( foo(6) ); // a: 6, b: 3, c:5
console.log( foo(8,2) ); // a: 8, b: 2, c:5
console.log( foo(1,2,3) );// a: 1, b: 2, c:3
</source>
省略された引数には undefind が入る。ここでは void 0 と比較している。
皮脂空の省略の処理は、ES6 以降は次のように書くことも出来る。
<source lang="javascript">
function foo(a = 2, b = 3, c = 5) {
return `a: ${a}, b: ${b}, c:${c}`;
}
console.log( foo() ); // a: 2, b: 3, c:5
console.log( foo(6) ); // a: 6, b: 3, c:5
console.log( foo(8,2) ); // a: 8, b: 2, c:5
console.log( foo(1,2,3) );// a: 1, b: 2, c:3
</source>
==== name ====
この関数の名前を返す。
<source lang="javascript">
function foo(a,b,c){ return; }
</
▲既に前の項で説明をしているが、指定されている引数が3つであるからいって、引数が3つである必要は無い。
=== メソッド ===
24 ⟶ 56行目:
<nowiki>
callメソッドの存在意義は、第1引数があり、ここで親オブジェクトを指定可能である所である。
34 ⟶ 66行目:
this.x=10;
(function(){
}).call(this);
} </nowiki>
|