What scope is 'this' of a node.js object when it gets called at the top-level?

前端 未结 3 1907
太阳男子
太阳男子 2021-01-27 06:03

I read \"Why and how to bind methods in your React component classes?\" and grabbed the basic idea of how different is this by the scopes it gets called.

I

3条回答
  •  故里飘歌
    2021-01-27 06:40

    const going = some.print;
    going(); // 'this' is undefined.
    

    Here, in the first line, you're storing a reference to the print function (property) of the some object in the variable going.

    Note, you're just storing the reference to that particular property (print), not along with the owner (some).

    this refers to the 'owner' always.

    So when you execute going, the owner (this) is unknown (or call it undefined).

提交回复
热议问题