我们知道,在一个铬窗,当执行下面的代码节点JS简单的功能和`this`
function test(){
console.log("function is " + this.test);
}
test();
test
被添加作为一个函数来window
对象,并将其显示
function is function test(){
console.log("function is " + this.test);
}
控制台中的
。
当我把相同的代码在一个文件sample.js,并执行使用node
nodejs$ node sample.js
它给
function is undefined
但是,当我直接在node
终端
nodejs$ node
> function test(){
... console.log("function is " + this.test);
... }test();
它显示与浏览器相同的输出。
任何人都可以解释为什么会发生这种情况以及V8引擎如何执行JavaScript文件?想更多地了解它,所以如果可能的话,请提供指向这篇文章和教程的链接。
放强调'is_function_test' – clyfe 2012-01-15 08:51:05