Water Sunlight

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

ユーザ用ツール

サイト用ツール


js:function:basic

差分

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

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

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
js:function:basic [2021/04/12 14:07]
tanaka
js:function:basic [2021/04/19 13:05] (現在)
tanaka [第1級オブジェクト]
行 5: 行 5:
 ===== 関数の定義 ===== ===== 関数の定義 =====
  
-以下による定義方法がある+以下による定義方法がある
   * function文   * function文
   * Functionコンストラクタ   * Functionコンストラクタ
   * 関数リテラル   * 関数リテラル
   * アロー関数[[js:top#ECMAScript|*2015]]   * アロー関数[[js:top#ECMAScript|*2015]]
 +\\
  
 ===== function文 ===== ===== function文 =====
行 20: 行 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>
行 34: 行 35:
 </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');
行 41: 行 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>
行 52: 行 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>
行 72: 行 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級オブジェクトである
   * 変数へ格納できる   * 変数へ格納できる
   * 引数にできる   * 引数にできる
行 89: 行 100:
   * 独自の存在をもつ(無名可能)   * 独自の存在をもつ(無名可能)
  
-例 +<sxh javascript;title:Example>
-<code javascript>+
 function func1(height, width){return height * width} function func1(height, width){return height * width}
                  
行 105: 行 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.1618204074.txt.gz · 最終更新: 2021/04/12 14:07 by tanaka