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

前端 未结 4 1404
后悔当初
后悔当初 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条回答
  •  暖寄归人
    2021-02-01 21:30

    In your example code there is no reason at all to copy this to a variable.

    It's usually used when the code uses a callback method. Inside the callback method this doesn't reference the object, so you use the variable for that.

提交回复
热议问题