Can you do this JS Interview Question?

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025

ความคิดเห็น • 8

  • @farhanerd
    @farhanerd 10 วันที่ผ่านมา +6

    That's a good example.
    An arrow function does not have its own this context and hence it always points out to the parsnts one ( in this case an object that is calling the function perimeter is under yhe global window object in terms of context and hence this refers to window)

  • @moisen8773
    @moisen8773 10 วันที่ผ่านมา +1

    Thanks for reminding me, I always forget that.

  • @hptechx
    @hptechx 6 วันที่ผ่านมา

    Arrow function has not it's own this
    In this context it's borrowed the context of parent environment of the current function 😅

  • @nikolozmchedlidze6197
    @nikolozmchedlidze6197 10 วันที่ผ่านมา +1

    Use regular function instead of arrow right? In arrow functions this refers to undefined

    • @averageguy7044
      @averageguy7044 10 วันที่ผ่านมา +2

      Arrow functions don't have their own this.
      They take this from the nearest enclosing regular function or global scope.

    • @himanshukumar-ls7fl
      @himanshukumar-ls7fl 8 วันที่ผ่านมา

      ​@@averageguy7044 In its lexical scope 👍

  • @krishnachaitanya7138
    @krishnachaitanya7138 10 วันที่ผ่านมา

    If I am not wrong "this" in an arrow don't even refer to window object I guess so always undefined or nan will be the answer

  • @xerox-f1p
    @xerox-f1p 4 วันที่ผ่านมา

    Then whats the solution