Here's a few fundamental facts you must understand to clarify the situation:
In the top-level code in a Node module, this is equivalent to module.exports. That's the empty object you see.
When you use this inside of a function, the value of this is determined anew before each and every execution of the function, and its value is determined by how the function is executed. This means that two invocations of the exact same function object could have different this values if the invocation mechanisms are different (e.g. aFunction() vs. aFunction.call(newThis) vs. emitter.addEventListener("someEvent", aFunction);, etc.) In your case, aFunction() in non-strict mode runs the function with this set to the global object.
When JavaScript files are required as Node modules, the Node engine runs the module code inside of a wrapper function. That module-wrapping function is invoked with a this set to module.exports. (Recall, above, a function may be run with an abitrary this value.)
Thus, you get different this values because each this resides inside a different function: the first is inside of the Node-created module-wrapper function and the second is inside of aFunction.
혹시나해서 이런거 해봤거든요 밖에서 var a = this하고 즉시실행함수에서 this.a = 50, 외부에서 a찍어보니까 {}
이 코드를 똑같은 버전의 노드로 실행하는데 이번엔 모듈을 로드한게 아니라 repl에 치면 a는 50으로 찍혀요
그니까 다른 모듈이면 this가 충돌하지 않도록 모듈마다 로드 될때 하나의 전역환경이 아니라 아닌 모듈에 한정된 전역환경을 주도록 로드될때 런타임에서 다른 확녕을 주는듯
저는 repl에서 실험한거라 모듈이라 할게 없어서 환경이 공유된거고 님은 node a.js로 모듈을 로드하는 과정이 었어서 차이가 생김
이건 js자체보단 커먼js가 어케 실행환경을 만들어주는지에 대해 알아야하는듯
repl에서는 그냥 global에, 소스 상에서는 modules.export라는 wrapper object안에 각종 변수들이 바인딩되게 되어있나 봅니다
저도 신기하네요 생각해보면 모듈마다 다른 전역환경을 가져야하니까 다른게 당연한거였음..
어떤 js 파일을 작성하면 결국 그게 하나의 모듈이 되야하니까 그렇겠네요. 이제 하시는 말씀 이해했습니다. ㄷㄷ
이 좆같은 언어에서 빨리 벗어나야 합니다