this in javascript

前端 未结 2 1976
轮回少年
轮回少年 2021-01-24 18:46

I\'m having a confusing problem using \'this\' in javascript. I have a method \'get_data\' which returns me some member variable of an object. Sometimes it returns to me the o

2条回答
  •  爱一瞬间的悲伤
    2021-01-24 19:25

    What 'this' is in JavaScript depends on how you call the function. If 'this' is not bound to an object, this will be the window object.

    If you call

    item = new Feed_Item()
    item.foo() //foo will be called with correct 'this'
    

    But if you do Feed_Item(some_data), you will add a couple of functions to the global window object.

    There are a lot of articles explaining this, e.g. http://www.digital-web.com/articles/scope_in_javascript/

提交回复
热议问题