javascript - 'this' inside an IIFE | Same as local scope? - Stack Overflow

If I have an IIFE does this refer to the local scope?(function(){var $a; $a = Su.$a this.$a = Su.$a

If I have an IIFE does this refer to the local scope?

(function(){

    var $a;   
    $a = Su.$a

    // this.$a = Su.$a; // can I replace with this

})();

I'm asking because I need Su.$a available everywhere in my IIFE.

But I don't want to call Su.$a, I want to call $a.

is saying this.$a the same as saying var $a when var it top-level scoped?

If I have an IIFE does this refer to the local scope?

(function(){

    var $a;   
    $a = Su.$a

    // this.$a = Su.$a; // can I replace with this

})();

I'm asking because I need Su.$a available everywhere in my IIFE.

But I don't want to call Su.$a, I want to call $a.

is saying this.$a the same as saying var $a when var it top-level scoped?

Share Improve this question edited Oct 24, 2012 at 16:37 hippietrail 17k21 gold badges109 silver badges179 bronze badges asked Aug 28, 2012 at 0:13 user656925user656925 0
Add a ment  | 

2 Answers 2

Reset to default 6

No.

this is set by a few things, described by MDN / this Operator, but in short:

  • the global object, at the top level scope
  • obj, when executing obj.func(...)
  • obj, when executing func.apply(obj, [...]) or func.call(obj, ...)
    or the global object, if obj is null or undefined
  • a new object with prototype func.prototype, when calling new func(...)
  • the event target, if elem.addEventListener('event', func, ...) and event is fired on elem

There's a few differences and additions in newer JavaScript, but that's pretty much it. this is unrelated to function scope.

No, they are different.

var $a, then the $a is local variable in the function scope.

But if you use this.$a, because this is a self execution function, this is window in this case, this.$a is same as window.$a, so you are using a global variable $a instead.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742394325a4435637.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信