function add(a, b) {
    return a + b;
};

add(1, 2); // 3
const add = function(a, b) {
    return a + b;
};

add(1, 2); // 3
const add = (a, b) => {
    return a + b;
};

add(1, 2); // 3
// 引数が1つだけの場合、丸括弧()は省略可能です。
const func = param => {...} // 丸括弧を省略した書き方

// 上記の表現は次のように解釈されます。
const func = (param) => {...} // 丸括弧を省略していない書き方
onst add = (a, b) => a + b; // returnキーワードを省略する場合、波括弧{}も必ず省略しなければなりません。

// 上記の表現は次のように解釈されます。
const add = (a, b) => { return a + b; };
const add = (a, b) => {
    const sum = a + b;
    console.log("Sum:", sum);
    
    // returnキーワードは省略できません。
    return sum;
};

const result = add(3, 5);
console.log("Result:", result); // 結果: Sum: 8, Result: 8
// 誤った使用例:重複した引数名
const multiply = (x, y, x) => {
    return x * y;
};
const Person = (name) => {
    this.name = name;
};

const john = new Person("John"); // エラー発生:Person is not a constructor
const Person = (name) => {
    this.name = name;
};

console.log(Person.hasOwnProperty("prototype")); // false
<!DOCTYPE html>
<html>
    <head>
        <title>Event Listener Example</title>
    </head>
    <body>
        <button id="myButton">Click Me</button>
        <script src="event.js"></script>
    </body>
</html>
const myButton = document.getElementById('myButton');

// 通常の関数を使ったイベントハンドラ
myButton.addEventListener("click", function() {
    console.log("通常関数でクリックされました。this: " + this.textContent);
});

// アロー関数を使ったイベントハンドラ
myButton.addEventListener("click", () => {
    console.log("アロー関数でクリックされました。thisのテキスト: " + this.textContent);
});
function sumWithArguments() {
    let sum = 0;
    
    for (let i = 0; i < arguments.length; i++) {
        sum += arguments[i];
    }
    
    return sum;
}

const sumWithArrow = () => {
    let sum = 0;
    
    for (let i = 0; i < arguments.length; i++) {
        sum += arguments[i]; // ここで arguments を使うとエラーになります。
    }
    
    return sum;
}

console.log(sumWithArguments(1, 2, 3, 4, 5)); // 出力: 15
console.log(sumWithArrow(1, 2, 3, 4, 5)); // エラー発生: arguments is not defined
const myObj = {
    name: "山田太郎",
    func: function() {
        console.log(this.name); // 出力: "山田太郎"

        /* 通常の関数を使用 */
        setTimeout(function() {
            console.log(this); // 出力: window オブジェクト(ブラウザ環境の場合)
            console.log(this.name); // エラーまたは undefined(出力されない)
        }, 1000);

        /* 通常の関数を使用 */
        setTimeout(function() {
            console.log(this); // 出力: myObj オブジェクト
            console.log(this.name); // 出力: "山田太郎"
        }.bind(this), 1000); // this を明示的に myObj にバインドする必要がある
    },
    arrow_func: function() {
        console.log(this.name); // 出力: "山田太郎"

        /* アロー関数を使用 */
        setTimeout(() => {
            console.log(this); // 出力: myObj オブジェクト
            console.log(this.name); // 出力: "山田太郎"
        }, 1000);
    }
};

myObj.func();
myObj.arrow_func();