this란

Wonhyo LEE·2022년 12월 12일
0
post-thumbnail

this 는 함수(메소드)를 호출한 객체를 가르킨다.

즉 this 는 자신의 위치에서 상위에 있는 객체를 가르키는것이다.

바인딩 시점은 함수 호출시 정해진다.

기본바인딩

console.log(this) // window
function test () {
  console.log(this) // window
}

암시적 바인딩

const test = {
	name:"test"
    fun:function test () {
     console.log(this) // {name:"test",fun:f}
    }
}

명시적 바인딩

function foo() {
    console.log(this.a);
}

const obj = { a: 1 };

foo.call(obj); // 1

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

add.call(null, 1, 2); // 3
add.apply(null, [1, 2]); // 3

new 바인딩

function Foo() {
    console.log(this);
}

new Foo(); // Foo
profile
프론트마스터를 꿈꾸는...

0개의 댓글