{"action":"opened","issue":{"url":"https://api.github.com/repos/wewea/wewea.github.io/issues/3","labels_url":"https://api.github.com/repos/wewea/wewea.github.io/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/wewea/wewea.github.io/issues/3/comments","events_url":"https://api.github.com/repos/wewea/wewea.github.io/issues/3/events","html_url":"https://github.com/wewea/wewea.github.io/issues/3","id":114181570,"number":3,"title":"[坑] javascript和node.js中global的区别","user":{"login":"wewea","id":6269927,"avatar_url":"https://avatars.githubusercontent.com/u/6269927?v=3","gravatar_id":"","url":"https://api.github.com/users/wewea","html_url":"https://github.com/wewea","followers_url":"https://api.github.com/users/wewea/followers","following_url":"https://api.github.com/users/wewea/following{/other_user}","gists_url":"https://api.github.com/users/wewea/gists{/gist_id}","starred_url":"https://api.github.com/users/wewea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wewea/subscriptions","organizations_url":"https://api.github.com/users/wewea/orgs","repos_url":"https://api.github.com/users/wewea/repos","events_url":"https://api.github.com/users/wewea/events{/privacy}","received_events_url":"https://api.github.com/users/wewea/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-10-30T01:43:14Z","updated_at":"2015-10-30T01:43:14Z","closed_at":null,"body":"1. javascript的top-level-scope是global scope, 但是在node.js中top-level-scope是被隐藏在module内的.\r\n\r\n同样的代码在node命令行和运行脚本的结果竟然不一样\r\n```javascript\r\n// interactive mode\r\nconsole.log(this === global) // => true\r\n\r\n// execute file\r\nconsole.log(this === global) // false\r\n```\r\n\r\n但是在node中在函数中如果有this, 直接进行函数调用仍然是绑定到global的\r\n```javascript\r\nfunction foo() {\r\n console.log(this === global);\r\n}\r\n\r\n```\r\n![download](https://cloud.githubusercontent.com/assets/6269927/10836564/2363f01a-7eea-11e5-85da-2bb46f22aa76.jpg)\r\n\r\n\r\n直到我RFTM\r\n> \r\nglobal#\r\n{Object} The global namespace object.\r\nIn 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.js this is different. The top-level scope is not the global scope; var something inside an *Node.js module will be local to that module.*"}}