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?