admin管理员组文章数量:1406926
I am trying to understand how Javascript works under the hood so I've been playing around a bit. For example, the following code generates different outputs when in Chrome's console or when in Node.js:
var length = 10;
function fn () {
console.log (this.length);
}
fn();
In Chrome, I get the value 10 logged, while in Node, this is undefined. Is it because in the window object, global variables are treated as properties of window, while in Node, the global object has a different behavior? If so, what is happening in the global object, and how could I get a similar behavior to window, without declaring global.length? I am doing this just as an exercise, there is no other purpose than understanding.
I am trying to understand how Javascript works under the hood so I've been playing around a bit. For example, the following code generates different outputs when in Chrome's console or when in Node.js:
var length = 10;
function fn () {
console.log (this.length);
}
fn();
In Chrome, I get the value 10 logged, while in Node, this is undefined. Is it because in the window object, global variables are treated as properties of window, while in Node, the global object has a different behavior? If so, what is happening in the global object, and how could I get a similar behavior to window, without declaring global.length? I am doing this just as an exercise, there is no other purpose than understanding.
Share Improve this question asked Feb 19, 2020 at 10:49 CiprianaCipriana 3932 gold badges4 silver badges15 bronze badges1 Answer
Reset to default 8The reason for the difference is that the top-level scope in a browser script is the global scope, but the top-level scope in a Node.js file is module scope. (By default it's an old-style Node.js module which is in loose mode, but in modern versions of Node.js you can opt into JavaScript standard module scope, which is in strict mode.)
At global scope, var
creates a property on the global object, which you can access via the window
global, via this
at global scope, or via this
in a loose-mode function you call without doing anything specific to set this
. Your call to fn
above is that last category: a loose-mode function called without setting this
.
But at Node.js module scope, var
just creates a var
in the module, it doesn't create a global or a property on the global object. It's like doing this:
(function() { // The wrapper Node.js provides node-style modules
// This is "module" scope
var foo = 10;
function fn () {
console.log (this.foo);
}
fn();
})(); // End of the wrapper
(Why did I change length
to foo
? Because by default, window
has a length
property [the number of frames the window contains], which confused matters. :-) )
So it's just a difference in scope.
You can also use module scope on modern browsers, including Chrome, via type="module"
on the script
tag. But since JavaScript standard modules are always in strict mode, the this
inside the call to fn
would be undefined
, causing an error:
<script type="module">
var length = 10;
function fn () {
console.log(this); // undefined
console.log(this.length); // TypeError
}
fn();
</script>
本文标签: javascriptDifference between window (browser) and global (Nodejs) objectsStack Overflow
版权声明:本文标题:javascript - Difference between window (browser) and global (Node.js) objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744975131a2635469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论