This code:
https://github.com/bjornharrtell/jsts/blob/master/index.js
refers to a variable named "GLOBAL".
When I run this in a browser, I get an error message:
"ReferenceError: GLOBAL is not defined"
Where does this GLOBAL come from, and why is it not defined?
In Node.js, GLOBAL is an alias to the global object (more commonly referred to as global).
In browsers, it doesn't exist.
Browserify recognizes the global alias, and will inject a reference to window, but does not recognize GLOBAL.
It was changed to global four years ago
Global variable came from the implementation of javascript in nodeJS
You can see it as Windows object for implementation of javascript in a web browser
The code you are looking at is designed to run in a Node.JS environment.
The GLOBAL variable is documented in the node manual.
It is a feature provided by the Node environment and not a core part of JavaScript.
Related
How is setting an environment variable like process.env.thing = 42 different from creating a global variable like global.thing = 42?
When would prefer process.env.thing over global? What are the pros/cons of both objects?
global is the global object. process is available globally, because it is a property of global. In fact:
global.process === process //-> true
process.env has properties set to the environment variables of the system. These can be set a variety of ways outside of node itself, and read in by accessing properties of process.env.
At the command line try:
FOO=bar node -e "process.env.FOO"
The process module is just a globally available thing.
The choice in my opinion must be something like this.
1)If the variable depends on the environment it must be set in process.env
2)If the variable is just a constant that is accessible from the entire application it must be set to global.
I think if you don't face these 2 points you don't have a need to store some value in both
If you start your node.js application you may want to use some different "environments", like API-URLs and stuff like this, because in a production / live environment those URLs are usually different in comparision to your local development environment.
This means that you can inject those paths using a .env file for example BEFORE starting your application.
This is an example:
NODE_API_URL=https://myApi.com/myEndpoints myApp.js
The global.thing = bla line will be read after the environment variables were set.
Once the application is running the environment variables and the other global definitions can be accessed by the app.
from the docs NodeAPI
The process object is a global that provides information about, and
control over, the current Node.js process. As a global, it is always
available to Node.js applications without using require().
You want to attach your environment variables to this object to make sure that there is no other pollution of globals.
The reason I ask is b.c. of the strange title for the MDN entry found here:
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout
WindowOrWorkerGlobalScope
I'm trying to understand why it is not listed under the window object?
Not all browser environments have a window object that corresponds to the global scope. Currently the various worker environments (Web, Shared, and Service) are good examples of these types of environments.
WindowOrWorkerGlobalScope defines a common set of methods shared by all browser global scopes. Window and WorkerGlobalScope define additional functionality on top of these methods.
This means, for instance, that you an access setTimeout from any browser JS environment but that open is only available in a window global scope and importScripts only in a worker environment.
For me variables are easy to understand in Javascript: if a variable is not in local scope then it is a field in the global object.
But what about Javascript commands? If i just write Javascript commands in a file (outside any function) then how will the Javascript engine interpret it?
----- file.js -----
console.log('hai:DDD');
--- end of file ---
Will it create some kind of "global" or "main" function type object with the commands and then execute it? What if there are multiple files with code?
I guess this question only applies to node.js because in browsers all Javascript code is event handlers
Javascript does not have a main function. It starts at the top and works it's way down to the bottom.
In Node.js, variables are stored in the module scope which basically means they're not quite global. In a way, you could imagine any code you run in Node.js as being wrapped up like this:
(function(exports, require, module, __filename, __dirname) {
...
})();
But you seem to have a misconception about the browser. Not all JS code is an event handler in the browser. If you just run a basic script in the browser it will also populate the global scope.
var myGlobal = "I'm available to everyone";
Javascript is, as the name implies, a scripting language to be interpreted by some Javascript interpreter. Thus, the "main function" can be thought of as the whole file, the entry point is at the first character of the first line in the script. Typically, the entirety of the functions the script is to perform are wrapped in a function that loads with the page, but this isn't necessary, just convenient.
There is no global function in JavaScript, but there are some similar concepts:
The Global Environment (10.2.3)
The global environment is a unique Lexical Environment which is
created before any ECMAScript code is executed. The global
environment’s Environment Record is an object environment
record whose binding object is the global object (15.1). The
global environment’s outer environment reference is null.
The Global Object (15.1)
An unique object used as the binding of the environment record of the global environment.
Global Code (10.4.1, 10.1)
Global code is source text that is treated as an ECMAScript Program. The global code of a particular Program does not include any source text that is parsed as part of a FunctionBody.
Global Execution Context (10.4.1.1)
An execution context of global code.
Here is a link to video.. watch this he explains how javascript works.
link to the video
And tool to visualize how JavaScript works.
link to the tool
In case if you want to run after the window is loaded there is window.onload , $(document).ready(); if you are using Jquery.
No, javascript is a scripting language, there is no point of insertion.
Lines of code are executed in the order they are encountered by the javascript interpreter.
If multiple files are included on the page, the functions and variables declared in them will be added to the global scope (unless they are declared in an anonymous function)
I don't know much about the JS console but I'm moving into command line programming and it would be good to know my native environment before I start using shell/node in a foreign one
My guess is that all command line statements are invoked with (window) eval(/* whatever user typed before hitting enter/*) but this seems like horrible practice. The context is definitely bound as "Window" if I console.log(this) but I'm not sure why / how
It's almost like the user cd'd into the window object to set it as the context
All JavaScript has a global scope. In the browser, the global scope is called "window" and it is a reference to the window object. In node, it is called "global" and refers to the global JS namespace in node.
The "window" global in a browser has a bunch of properties on it. Things like "console" or "location". When you want to talk to those pieces of code, you can optionally prefix the call with "window.". Example: to log something to the console you can type:
window.console.log("HELLO");
or you can leave the "window." off of the call, like such:
console.log("HELLO");
The global "window" object in the browser is the only optional object on the page. Any time you see a reference to a variable that wasn't declared using "var" (or "let" and "const" in ES6), that means that the variable is just a property on the global object.
In Javascript: The Good Parts Crockford writes that "Javascript depends on the global variable for linkage." I understand that in javascript if you declare a variable outside a function it "is in the global namespace" -- meaning you can access it anywhere in the program. I sort of understand how linkage limits where in a C++ program you can access a variable (regardless of its scope). With that said, what does Crockford mean?
I think what he means is that global variables are how you communicate with libraries. For example jquery uses the global variable $, underscore uses _, etc. You link to the libs through a global name.
Continuing
All the top-level variables of all compilation units are tossed
together in a global namespace called the global object. This is a bad
thing because global variables are evil, and in JavaScript they are
fundamental. Fortunately, as we will see, JavaScript also gives us the
tools to mitigate this problem.
What Crockford is referring to, I think, is the absence of module or namespace-like mechanisms in JS to segregate and modularize chunks of functionality at a macro level, where those chunks are explicit about what they are exposing (export), or what they are using from other chunks (import). However, as he points out, JS does provide the ability, albeit imperfectly, to do this through existing language features, and the world has not come to an end. At the same time, this problem is now being addressed on a number of fronts, notably the ES6 module mechanism.