index.html
<!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"');
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"');
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>
jsbin.casasuc.js
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"');
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.');