LoginSignup
0
0

More than 3 years have passed since last update.

【Javascript】関数の引数の使い方

Posted at

⑴記事の背景
学習の一環として、【Todoリスト】を作成していく中である関数の外部で中で定めた定数を外の関数でも使用したいときにどのようにすれば使えるのか教えてもらい、それを忘れない為。備忘録。

引数とは:プログラムやある特定の数値などで、関数に渡すもの

書き方:
const 関数名  = function(引数名1,引数名2){
処理(例:return 引数1 + 引数2)
};
console.log(3,5) ▶️ 8
引数名1や引数名2などは、自分で好きな名前を付けられる。
実際に関数を使うときに具体的な名前や数値などを入力すれば良い。

<Todoリスト作成時に使った内容>


 for (let i = 0; i < radioBtn.children.length; i++) { 

             if (radioBtn.children[i].checked === true) { 
                 changeTodoDisplay(radioBtn.children[i].value); 
                 console.log(radioBtn.children[i].value); 
             } 
         } 

const changeTodoDisplay = function (radioBtnState) { 
        console.log(radioBtnState); 
        if (radioBtnState === 'all') { 
             todoShow(todos); 
       }; 
         if (radioBtnState === 'working') { 
             const filterTodo = todos.filter(function (todo) { 
                 return todo.stateBtn.textContent === "作業中"; 
             }); 
             todoShow(filterTodo); 
             console.log(filterTodo); 
         }; 
         if (radioBtnState === 'complete') { 
             const filterTodo = todos.filter(function (todo) { 
                 return todo.stateBtn.textContent === "完了"; 
             }) 
             todoShow(filterTodo); 
         }; 
     } 

今回、forの中で取り出したvalueを具体的な引数(radioBtn.children[i].value)として渡して、関数の中で使っている。 ※radioBtnState=radioBtn.children[i].value

引数を使う事で、ある関数の中で定めた定数でも、外の関数の中で引数として渡して使用することができる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0