「JavaScript」bindを使用してthisの値を指定する

2021年10月12日

書式
関数名.bind(引数)
bind() メソッドは、呼び出された際に this キーワードに指定された値が設定される
新しい関数を生成します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<script>
function funA() {
alert(this);
}
//bindメソッドの引数に新たな関数を生成
const funB = funA.bind("study skill in arkgame");
// thisの内容を出力
funB();
</script>
<script> function funA() { alert(this); } //bindメソッドの引数に新たな関数を生成 const funB = funA.bind("study skill in arkgame"); // thisの内容を出力 funB(); </script>
<script>
  function funA() {
    alert(this);
  }
   //bindメソッドの引数に新たな関数を生成  
  const funB = funA.bind("study skill in arkgame");
  // thisの内容を出力
  funB();
</script>

結果
study skill in arkgame

JavaScript

Posted by arkgame