機能拡張の方向でいくつかの新機能も追加されており、これらの機能も非常に重要だと感じています
1.パラメータのデフォルト値(注:デフォルト値のないパラメータは、デフォルト値)
1 2 3 4 5 6 | { function test(x, y = 'world' ){
console.log( '默认值' ,x,y);
}
test( 'hello' );
test( 'hello' , 'kill' );
}
|
ログイン後にコピー
1 2 3 4 5 6 | {
let x= 'test' ; function test2(x,y=x){
console.log( '作用域' ,x,y);
}
test2( 'kill' );
}
|
ログイン後にコピー
2. 残りのパラメータ (...) は、一連の離散値を配列に変換します
1 2 3 4 5 6 | { function test3(...arg){ for (let v of arg){
console.log( 'rest' ,v);
}
}
test3(1,2,3,4, 'a' );
}
|
ログイン後にコピー
3. スプレッド演算子 (...) は a を変換します 配列は一連の離散値に変換されます
1 2 3 4 | {
console.log(...[1,2,4]);
console.log( 'a' ,...[1,2,4]);
}
|
ログイン後にコピー
IV.分かりました!!!) 例えば、 a=>a*2 a がパラメータ、a*2 が戻り値 => 関数の記号としてパラメータを渡さない場合は ( と表現できます。 )
1 2 3 4 5 6 7 | {
let arrow = v => v*2;
let arrow2 = () => 5;
console.log( 'arrow' ,arrow(3));
console.log(arrow2());
}
|
ログイン後にコピー
5. 末尾呼び出し: 関数が別の関数をネストする場合は、末尾呼び出しを検討できます
1 2 3 4 5 6 | { function tail(x){
console.log( 'tail' ,x);
} function fx(x){ return tail(x)
}
fx(123)
}
|
ログイン後にコピー
以上がES6での機能拡張のチュートリアル例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。