1

As I understand, when a variable is defined it gets attached to the 'closest' local scope via this.

If no scope is set locally, the closest scope becomes window.

In strict mode, however, the local scope is set to undefined instead of window, as part of the ECMAscript 5 spec designed to limit misuse of the global scope.


Using an immediately invoked function expression pattern and strict mode for creating a jQuery plugin

;( function( $, window ){
   "use strict";
   var myScope = this;
   var myVar = 1;

   var myFunction = function(){
     console.log( myScope, myVar );
   } 

   $.myFunc = myFunction;

})( jQuery, window );

the local scope (context) isn't created (via a function call) and thus set to undefined.

If the local scope is undefined and window.myVar is undefined, what is the scope of the variable myVar and how do you access it?

Community
  • 1
  • 1
qodeninja
  • 10,946
  • 30
  • 98
  • 152

2 Answers2

2

The this context variable has not anything to do with Scope, in terms of how ECMAscript implements Lexical scoping underneath.

this will always reference the object of invocation, which can change during run-time, whereas the scope / context cannot get changed. You got two function invocations there, each of those has its own Execution Context and its own Activation Object respectively Lexical Environment Record, which are used by the implementation to store any local data like formal paramteres, variables and function declarations.


What you are actually trying to achieve here, is to create a hash / namespace object which serves as container to hold data. Therefore, you shouldn't assign this, but just create your own Object.

var myScope = { };  // respectively Object.create( null );

In strict mode, the implementation will just set this to undefined for any function which got invocated as is. That means, the value of this is decided of how a function gets called. In your case, its a self-executing function and hence, its this will always be undefined in strict mode and window (global) in non strict mode.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • I'm asking what is the object context of myVar. I guess since they simply decided to not set it to window, the answer is precisely, undefined. I thought that assigning a value to a variable implied there was some context that I should have access to, and if it's not `this` or `window`, then it should be something. – qodeninja Jan 19 '13 at 19:29
  • @qodeninja: exactly. It still depends on how a function gets called, but in your case, the `this` value will always contain `undefined` in strict mode. – jAndy Jan 19 '13 at 19:31
  • @qodeninja: your same code without 'use strict' will also not assign the variable to window, but the underlying context doesn't change between the two situations. In other words, without strict, you still wouldn't be able to do `window.myVar` or `myScope.myVar` – Jim Deville Jan 19 '13 at 19:34
  • @JimDeville: of course he would ! Without *strict mode*, `this` would just be a reference to `window` (if I got you right). He won't be able to access the variable `myVar` from "outside", but he would of course be able to access anything he wrote into the global object via that reference. – jAndy Jan 19 '13 at 19:36
  • @jAndy: but since it is module scoped, and `myVar` is declared with `var`, it won't get exposed on `this`. IOW, you still cannot do `myScope.myVar` or `window.myVar` – Jim Deville Jan 19 '13 at 19:39
  • @JimDeville: oh... no I got it. Yes you're right with that of course. What I meant was, in *non strict mode*, he could do like `myVar.foo = 42;` and would be able to access `window.foo` from anywhere else, since both reference the *global object*. – jAndy Jan 19 '13 at 19:53
  • ah, yes (assuming you meant `myScope`, not `myvar` in that comment), on that, we agree. – Jim Deville Jan 19 '13 at 19:54
1

myVar is scoped to the containing closure. When I run your code, and then run $.myFunc(), the output is undefined 1, since myVar is still present in that closure. There is, however, no way to access myVar from outside of the closure. That is the same mechanism used to implement private variables in JS.

What @jAndy is saying is also true, this is unrelated to scope, and in this case the this of the anonymous function is undefined

Jim Deville
  • 10,632
  • 1
  • 37
  • 47