Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining an implementation independent version of the global object in JavaScript

I'm trying to define the global object in JavaScript in a single line as follows:

var global = this.global || this;

The above statement is in the global scope. Hence in browsers the this pointer is an alias for the window object. Assuming that it's the first line of JavaScript to be executed in the context of the current web page, the value of global will always be the same as that of the this pointer or the window object.

In CommonJS implementations, such as RingoJS and node.js the this pointer points to the current ModuleScope. However, we can access the global object through the property global defined on the ModuleScope. Hence we can access it via the this.global property.

Hence this code snippet works in all browsers and in at least RingoJS and node.js, but I have not tested other CommomJS implementations. Thus I would like to know if this code will not yield correct results when run on any other CommonJS implementation, and if so how I may fix it.

Eventually, I intend to use it in a lambda expression for my implementation independent JavaScript framework as follows (idea from jQuery):

(function (global) {
    // javascript framework
})(this.global || this);
like image 232
Aadit M Shah Avatar asked Nov 26 '11 17:11

Aadit M Shah


People also ask

How do you declare a global object in JavaScript?

A global object is an object that always exists in the global scope. In JavaScript, there's always a global object defined. In a web browser, when scripts create global variables defined with the var keyword, they're created as members of the global object. (In Node.

How do you define a global function in JavaScript?

Open JS console, write var x = function() {console. log('x')} and then try to call window. x() . In browsers, window is global scope, therefore the function is global.

What is the name of the global object that the browser assigns to JavaScript?

Global Variables in HTML With JavaScript, the global scope is the JavaScript environment. In HTML, the global scope is the window object.

What is the default global object in JavaScript?

The global object provides variables and functions that are available anywhere. By default, those that are built into the language or the environment. In a browser it is named window , for Node. js it is global , for other environments it may have another name.


1 Answers

this is in no way relevant to scope.

(function(){
    (function(){
        (function(){

            (function(){
            alert( this ); //global object
            })()

        }).bind({})()
    }).apply({})
}).call({})

this is only resolved during the call time of the function and comes down to few simple rules.

  1. If the function is called as a property of some object, then that object will be this inside the function
  2. If the function is called as is, this will be undefined so under non strict mode it will be the global object
  3. If the function is called with .call/.apply then this is explicitly set by yourself.

So as you can see, it would fall under rule #2, which resolves to undefined. And since there is no "use strict";:

set the ThisBinding to the global object

Edit: I have now ran some quick tests in RingoJS and they infact put the "global object" inside the actual global object (as defined by standards) which is ModuleScope. Just because the actual global object in most js implementations has Object and String and so on, doesn't make an object global if it has those objects under it as well. The reason you can access String and Object in RingoJS is because they put them into the ModuleScope prototype:

var logs = require('ringo/logging').getLogger("h");

logs.info( Object.getPrototypeOf( this ) === this.global );
//true

Further proof that ModuleScope is the actual global object:

this.property = "value";
logs.info( property );
//"value"

So nothing is gained from this kind of trickery, it doesn't fix anything:

function injectGlobal(){
globalProperty = "value"; // "use strict" would fix this!
}

injectGlobal()

logs.info( globalProperty );
//"value"

Rant over, this refers to the actual global object already according to the rules given earlier in this post. this.global is not the real global object as defined by standards, it's just a container.

Additionally you could emulate this behavior in browsers:

Consider scopehack.js

this.global = window.global || top.global || {};

Consider main.html:

<script src="scopehack.js"></script>
<script>
this.global.helloWorld = "helloWorld"; //"global scope"
this.helloWorld = "helloWorld" //"ModuleScope"
</script>

<iframe src="module.html"></iframe>

And finally a "module" module.html:

<script src="scopehack.js"></script>
<script>
    with( this.global ) { //poor mans RhinoJS scope injection, doesn't work for writing
        console.log( helloWorld ); //"global scope" - "helloWorld"
        console.log( this.helloWorld ); //"ModuleScope" undefined
    }
</script>

Which one is an actual global object in both module.html and main.html? It is still this.

TLDR:

var obj = {
"String": String,
"Object": Object,
.....
};

Does not make obj the global object.

like image 184
Esailija Avatar answered Oct 14 '22 01:10

Esailija