Water Sunlight

軽量プログラミングの文法 - JavaScript/Python

ユーザ用ツール

サイト用ツール


js:function:basic

差分

このページの2つのバージョン間の差分を表示します。

この比較画面にリンクする

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
js:function:basic [2021/04/05 10:23]
tanaka
js:function:basic [2021/04/19 13:05] (現在)
tanaka [第1級オブジェクト]
行 3: 行 3:
  --- //[[http://www.y2sunlight.com/water|y2sunlight]] 2021-04-05//  --- //[[http://www.y2sunlight.com/water|y2sunlight]] 2021-04-05//
  
-=== 関数の定義 === +===== 関数の定義 ===== 
-以下による定義方法がある+ 
 +以下による定義方法がある
   * function文   * function文
   * Functionコンストラクタ   * Functionコンストラクタ
   * 関数リテラル   * 関数リテラル
   * アロー関数[[js:top#ECMAScript|*2015]]   * アロー関数[[js:top#ECMAScript|*2015]]
 +\\
  
-=== function文 ===+===== function文 =====
  
 <code javascript> <code javascript>
-function 関数名(引数, ) {+function 関数名(引数, ・・・) {
     // 本体     // 本体
     return 戻り値;     return 戻り値;
行 19: 行 21:
 </code> </code>
  
-例 +<sxh javascript;title:Example>
-<code javascript>+
 console.log(getArea(10, 20));    // 結果:200 console.log(getArea(10, 20));    // 結果:200
 function getArea(height, width) { function getArea(height, width) {
     return height * width;     return height * width;
 } }
-</code>+</sxh> 
 +\\
  
-=== Functionコンストラクタ ===+===== Functionコンストラクタ =====
  
 <code javascript> <code javascript>
-var 変数名 = new Function(引数, , 関数本体);+var 変数名 = new Function(引数, ・・・, 関数本体);
 </code> </code>
  
-例 +<sxh javascript;title:Example>
-<code javascript>+
 var func1 = new Function('height', 'width', 'return height * width'); var func1 = new Function('height', 'width', 'return height * width');
 var func2 = new Function('height, width', 'return height * width'); var func2 = new Function('height, width', 'return height * width');
行 40: 行 41:
 console.log(func1(10, 20));  // 結果:200 console.log(func1(10, 20));  // 結果:200
 console.log(func2(10, 20));  // 結果:200 console.log(func2(10, 20));  // 結果:200
-</code>+</sxh> 
 +\\
  
-=== 関数リテラル ===+===== 関数リテラル =====
  
 <code javascript> <code javascript>
-var 変数名 = function(引数, ) {+var 変数名 = function(引数, ・・・) {
     // 関数本体     // 関数本体
     return 戻り値;     return 戻り値;
行 51: 行 53:
 </code> </code>
  
-例 +<sxh javascript;title:Example>
-<code javascript>+
 var func = function(height, width) { var func = function(height, width) {
     return height * width;     return height * width;
 }; };
 console.log(func(10, 20));  // 結果:200 console.log(func(10, 20));  // 結果:200
-</code> +</sxh>
- +
-関数リテラルは、無名関数又は匿名関数とも呼ばれる+
  
 +関数リテラルは、無名関数又は匿名関数とも呼ばれる。
 +\\ \\
  
-** アロー関数<sup>[[js:top#ECMAScript|*2015]]</sup> **+===== アロー関数 ===== 
 +<sup>[[js:top#ECMAScript|*2015]]</sup>
  
 <code javascript> <code javascript>
-var 変数名 = (引数, ) => {+var 変数名 = (引数, ・・・) => {
     // 関数本体     // 関数本体
     return 戻り値;     return 戻り値;
行 71: 行 73:
 </code> </code>
  
-例 +<sxh javascript;title:Example>
-<code javascript>+
 var func = (height, width) => { var func = (height, width) => {
     return height * widht;     return height * widht;
 }; };
 console.log(func(10, 20)); console.log(func(10, 20));
-</code>+</sxh> 
 + 
 +<sxh javascript;title:Example 括弧の省略> 
 +var area = r => Math.PI * r * r; 
 +console.log(area(10)); 
 +</sxh> 
 + 
 +<sxh javascript;title:Example 引数無し> 
 +var sayHello = () => console.log('Hello'); 
 +sayHello(); 
 +</sxh> 
 +\\
  
-=== 第1級オブジェクト ===+===== 第1級オブジェクト =====
  
-JavaScriptの関数は第1級オブジェクトである+JavaScriptの関数は第1級オブジェクトである
   * 変数へ格納できる   * 変数へ格納できる
   * 引数にできる   * 引数にできる
行 88: 行 100:
   * 独自の存在をもつ(無名可能)   * 独自の存在をもつ(無名可能)
  
-例 +<sxh javascript;title:Example>
-<code javascript>+
 function func1(height, width){return height * width} function func1(height, width){return height * width}
                  
行 104: 行 115:
 callFunc(3, 10, func3);  // 結果:30 callFunc(3, 10, func3);  // 結果:30
 callFunc(4, 10, func4);  // 結果:40 callFunc(4, 10, func4);  // 結果:40
-</code>+</sxh>
js/function/basic.1617585825.txt.gz · 最終更新: 2021/04/05 10:23 by tanaka