JavaScript アロー関数でthisの参照先を利用するサンプル

環境
Windows 10 home 64bit
Google Chrome 107.0.5304.122

書式
param => expression
引数が単一の場合。単純な式ならば return は不要です。

params => ({foo: “a"})
オブジェクトリテラル式を返す場合は、式の周りに括弧が必要です。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
this.str = 'study skill';
const cft = () => {
console.log(this.str);
}
let objA = {
str: 'arkgame',
name: cft
}
objA.name();
this.str = 'study skill'; const cft = () => { console.log(this.str); } let objA = { str: 'arkgame', name: cft } objA.name();
this.str = 'study skill';

const cft = () => {
  console.log(this.str);
}

let objA = {
  str: 'arkgame',
  name: cft
}

objA.name();

実行結果
> “study skill"

JavaScript

Posted by arkgame