Skip to content

Instantly share code, notes, and snippets.

@shreshthmohan
Created February 9, 2018 11:39
Show Gist options
  • Save shreshthmohan/8854daa64ff4f00cc2234abd425f8107 to your computer and use it in GitHub Desktop.
Save shreshthmohan/8854daa64ff4f00cc2234abd425f8107 to your computer and use it in GitHub Desktop.
Global - Execution Context, Object // source http://jsbin.com/casasuc
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Global - Execution Context, Object</title>
</head>
<body>
<p>Look in the console.</p>
<p><a href="https://stackoverflow.com/questions/34838659/the-this-keyword-behaves-differently-in-nodejs-and-browser" target="_blank"><b>this</b> behaves diffrently in the browser vs Node.js</a></p>
<script id="jsbin-javascript">
console.log(this);
console.log(this === window);
console.log('The base/global execution constant creates two things, a global object (objects are simply name value pairs, remember?) and *this*. this is a special variable created by the global execution environment. this is also equal to the global object. Inside the broswer window is the global object. When we say global, that simply means "not inside a function"');
// all of the code here is global.
var a = 5;
var b = function(x) {
return x * 2;
}
console.log('this.a:', this.a);
console.log('this.b:', this.b);
console.log('If you look inside the global/window object you\'ll see a and b as it\'s properties.');
if (this === window) {
console.log('this is same a window object');
}
console.log('There would be a different window/global object in another browser window. Each tab is a different execution context and so it would have it\'s own global/window object.');
</script>
<script id="jsbin-source-javascript" type="text/javascript">console.log(this);
console.log(this === window);
console.log('The base/global execution constant creates two things, a global object (objects are simply name value pairs, remember?) and *this*. this is a special variable created by the global execution environment. this is also equal to the global object. Inside the broswer window is the global object. When we say global, that simply means "not inside a function"');
// all of the code here is global.
var a = 5;
var b = function(x) {
return x * 2;
}
console.log('this.a:', this.a);
console.log('this.b:', this.b);
console.log('If you look inside the global/window object you\'ll see a and b as it\'s properties.');
if (this === window) {
console.log('this is same a window object');
}
console.log('There would be a different window/global object in another browser window. Each tab is a different execution context and so it would have it\'s own global/window object.');
</script></body>
</html>
console.log(this);
console.log(this === window);
console.log('The base/global execution constant creates two things, a global object (objects are simply name value pairs, remember?) and *this*. this is a special variable created by the global execution environment. this is also equal to the global object. Inside the broswer window is the global object. When we say global, that simply means "not inside a function"');
// all of the code here is global.
var a = 5;
var b = function(x) {
return x * 2;
}
console.log('this.a:', this.a);
console.log('this.b:', this.b);
console.log('If you look inside the global/window object you\'ll see a and b as it\'s properties.');
if (this === window) {
console.log('this is same a window object');
}
console.log('There would be a different window/global object in another browser window. Each tab is a different execution context and so it would have it\'s own global/window object.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment