this keyword in js 작성중...

Yoseob Shin·2022년 8월 31일
0

javascript

목록 보기
22/24
post-custom-banner

The key to understanding this keyword is having a clear view of function invocation and how it impacts the context.

This article focuses on the invocation explanation, how the function invocation influences this and demonstrates the common pitfalls of identifying the value of this.

this in indirect invocation

this in nested function

var colours = ['red', 'green', 'blue'];
document.getElementById('element').addEventListener('click', function() {
    // this is a reference to the element clicked on

    var that = this;

    colours.forEach(function() {
        // this is undefined
        // that is a reference to the element clicked on
    });
});

that === this ?

var colours = ['red', 'green', 'blue'];
document.getElementById('element').addEventListener('click', function() {
    // this is a reference to the element clicked on

    var that = this;

    colours.forEach(function() {
        // this is undefined
        // that is a reference to the element clicked on
    });
});

Because this frequently changes when you change the scope by calling a new function, you can't access the original value by using it. Aliasing it to that allows you still to access the original value of this.


this
https://dmitripavlutin.com/gentle-explanation-of-this-in-javascript/#71-this-in-arrow-function

context of this with constructor function invocation without new operator.
https://stackoverflow.com/questions/42745994/is-it-possible-to-call-constructor-function-method-without-creating-object-in-js

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#see_also

https://velog.io/@04_miffy/%EC%83%9D%EC%84%B1%EC%9E%90%EC%99%80-this

https://kim-solshar.tistory.com/57

profile
coder for web development + noodler at programming synthesizers for sound design as hobbyist.
post-custom-banner

0개의 댓글