what's the difference between Browsers and Node? for instance:
浏览器和节点之间有什么区别?例如:
setName.js
on Node:
节点上的setName.js:
var setName;
setName = function (name) {
return this.name = name;
};
setName("LuLu");
//LuLu
console.log(name);
//undefined
console.log(this.name);
setName.html
in browser:
浏览器中的setName.html:
<script>
var setName;
setName = function (name) {
return this.name = name;
};
setName("LuLu");
//LuLu
console.log(name);
//LuLu
console.log(this.name);
</script>
the the second log is different,why?
第二个日志不同,为什么?
2 个解决方案
#1
8
Node is a JavaScript engine, not a browser. The specific reason that you see undefined
in Node, and Lulu
in a browser? Differences in the global namespace:
Node是JavaScript引擎,而不是浏览器。您在Node中看到未定义的特定原因,以及浏览器中的Lulu?全局命名空间的差异:
In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope
var something
will define a global variable. In Node this is different. The top-level scope is not the global scope;var something
inside a Node module will be local to that module.在浏览器中,*范围是全局范围。这意味着在浏览器中,如果您处于全局范围,则var将定义全局变量。在Node中,这是不同的。*范围不是全球范围; Node模块中的var内容将是该模块的本地内容。
In the browser, this
is a reference to the window
object — the browser's global namespace — for all functions which are invoked unattached to an object (e.g. not like foo.bar()
). In Node, this
is simply not a reference to the global namespace.
在浏览器中,这是对窗口对象(浏览器的全局命名空间)的引用,用于所有未附加到对象的函数(例如,不像foo.bar())。在Node中,这不是对全局命名空间的引用。
N.B. console.log(this.name)
in a Node interpreter will print Lulu
, not undefined
. That's because, in the REPL only,
注:节点解释器中的console.log(this.name)将打印Lulu,而不是未定义。那是因为,仅在REPL中,
> this === global
true
Further reading @ How To Node: What is "this?"
进一步阅读@如何节点:什么是“这个?”
Okay, one more edit as prompted by @Šime Vidas' comment regarding this
in ES5 strict mode:
好的,在ŠimeVidas关于ES5严格模式的评论提示的另一个编辑:
- In the global context (outside of any function),
this
refers to the global object, whether in strict mode or not.在全局上下文中(在任何函数之外),这指的是全局对象,无论是否处于严格模式。
- When the
this
keyword occurs inside a function, its value depends on how the function is called.当this关键字出现在函数内部时,其值取决于函数的调用方式。
- When a function is called as a method of an object, its
this
is set to the object the method is called on.当一个函数作为一个对象的方法被调用时,它被设置为调用该方法的对象。
More interesting reading courtesy of Juriy Zaytsev (aka @kangax) in one of his blog posts.
更有趣的阅读由Juriy Zaytsev(又名@kangax)在他的一篇博客文章中提供。
#2
2
Your browser code has the window
host object. Node does not have that host object. When you set this.name
, you are actually setting it to the window
object aka making a global variable.
您的浏览器代码具有窗口主机对象。节点没有该主机对象。设置this.name时,实际上是将其设置为窗口对象,即创建全局变量。
window.name === this.name // true
window.name === this.name // true
#1
8
Node is a JavaScript engine, not a browser. The specific reason that you see undefined
in Node, and Lulu
in a browser? Differences in the global namespace:
Node是JavaScript引擎,而不是浏览器。您在Node中看到未定义的特定原因,以及浏览器中的Lulu?全局命名空间的差异:
In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope
var something
will define a global variable. In Node this is different. The top-level scope is not the global scope;var something
inside a Node module will be local to that module.在浏览器中,*范围是全局范围。这意味着在浏览器中,如果您处于全局范围,则var将定义全局变量。在Node中,这是不同的。*范围不是全球范围; Node模块中的var内容将是该模块的本地内容。
In the browser, this
is a reference to the window
object — the browser's global namespace — for all functions which are invoked unattached to an object (e.g. not like foo.bar()
). In Node, this
is simply not a reference to the global namespace.
在浏览器中,这是对窗口对象(浏览器的全局命名空间)的引用,用于所有未附加到对象的函数(例如,不像foo.bar())。在Node中,这不是对全局命名空间的引用。
N.B. console.log(this.name)
in a Node interpreter will print Lulu
, not undefined
. That's because, in the REPL only,
注:节点解释器中的console.log(this.name)将打印Lulu,而不是未定义。那是因为,仅在REPL中,
> this === global
true
Further reading @ How To Node: What is "this?"
进一步阅读@如何节点:什么是“这个?”
Okay, one more edit as prompted by @Šime Vidas' comment regarding this
in ES5 strict mode:
好的,在ŠimeVidas关于ES5严格模式的评论提示的另一个编辑:
- In the global context (outside of any function),
this
refers to the global object, whether in strict mode or not.在全局上下文中(在任何函数之外),这指的是全局对象,无论是否处于严格模式。
- When the
this
keyword occurs inside a function, its value depends on how the function is called.当this关键字出现在函数内部时,其值取决于函数的调用方式。
- When a function is called as a method of an object, its
this
is set to the object the method is called on.当一个函数作为一个对象的方法被调用时,它被设置为调用该方法的对象。
More interesting reading courtesy of Juriy Zaytsev (aka @kangax) in one of his blog posts.
更有趣的阅读由Juriy Zaytsev(又名@kangax)在他的一篇博客文章中提供。
#2
2
Your browser code has the window
host object. Node does not have that host object. When you set this.name
, you are actually setting it to the window
object aka making a global variable.
您的浏览器代码具有窗口主机对象。节点没有该主机对象。设置this.name时,实际上是将其设置为窗口对象,即创建全局变量。
window.name === this.name // true
window.name === this.name // true