3-01.this 키워드

2023. 3. 29. 16:43Javascript/자바스크립트 제대로 배워볼래?

 console.log(this);

를 하게 된다면, window가 출력된다.

 

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
</head>
<body>
    <button type="buton" onclick="this.style.backgroundColor='red'">클릭</button>
    <button type="buton" onclick="callFunc(this);">클릭2</button>
    <select onchange="changeSelect(this);">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <script>
        console.log(this);

        function fullName(){

        }
        var person = {
            firstName:"John",
            lastName: "Doe",
            fullName: function(){
                return this.firstName + " " + this.lastName;
            }
        }
        console.log(person.fullName());


        // function안이던, 밖이던 window객체를 반환
        function myFunction() {
            console.log(this);
        }
        myFunction();

        function callFunc(obj){
            console.log(obj)
        }
        
        function changeSelect(obj) {
            console.log(obj.value);
        }
    </script>
</body>
</html>

'Javascript > 자바스크립트 제대로 배워볼래?' 카테고리의 다른 글

3-03.Default Function Parameter  (0) 2023.03.29
3-02.Scope  (0) 2023.03.29
2-09.크롬 개발자도구  (0) 2023.03.29
2-08.Window 객체  (0) 2023.03.29
2-07.JSON 객체  (0) 2023.03.29