Why do we need “var self = this” in classes in Javascript?

前端 未结 4 1444
后悔当初
后悔当初 2021-02-01 20:54

Why can\'t we directly use this instead of self in the following example?

function SeatReservation(name, initialMeal) {
    var self =          


        
4条回答
  •  旧时难觅i
    2021-02-01 21:29

    It is usually done in order to keep a reference to this when the context is changing. It is often used in event handlers or callback functions. But as mentioned before, there is no reason to use it in your specific example.

    You will find more details in the following article: http://www.alistapart.com/articles/getoutbindingsituations

提交回复
热议问题