JavaScript this refers to window instead of object inside function

前端 未结 3 1996
既然无缘
既然无缘 2020-12-06 06:26

I get confused on a JavaScript this reference situation.

I am working on a code that I declare function inside an object method. (The reason is to tidy

相关标签:
3条回答
  • 2020-12-06 06:27

    this has nothing to do with scope. It is determined by context.

    greeting() calls the function with no context, so this is the default object (window in a browser).

    0 讨论(0)
  • 2020-12-06 06:28

    The this, references is not related to scope, it depends on the calling context.

    As per the MDN doc,

    In general, the object bound to this in the current scope is determined by how the current function was called

    0 讨论(0)
  • 2020-12-06 06:52

    Try person.nickname, this refers to the var greeting in your case

    0 讨论(0)
提交回复
热议问题