Replies: 1 comment
-
| 📝 調べ方としては  まだちゃんとは調べていませんが、Node.jsの また、CommonJSファイルの実行はReflect.apply()( https://github.com/nodejs/node/blob/cebbc57ed26ce1a572e6fe416b700ac8f6258869/lib/internal/modules/cjs/loader.js#L253-L256 これは、次のようなファイルを  "use strict";
exports.test = 1;
function funcThis() {
  return this;
}
console.log(funcThis()); // undefined
const arrowThis = () => {
  return this;
};
console.log(arrowThis()); // { test: 1 }
console.log(arrowThis() === this); // true
console.log(arrowThis() === globalThis); // false同じように REPLでも 
 https://github.com/nodejs/node/blob/78d280a768219a6af484b7ecbf3e836461b57477/lib/vm.js#L124-L130 V8をコードとEnvを渡して呼び出していて、おそらくEnvにthisの値となるものが指定されてると思うので、この辺を追うとわかるような気がします。 | 
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
-
Node REPL と ファイルを実行したときに、
thisの戻り値が違うのが気になっています。該当ページ
アロー関数の実行について
https://jsprimer.net/basic/function-this/#arrow-function-this
質問
以下は
node ファイル名.jsで実行した結果です。しかし、Node REPL で実行すると以下の結果になり、
arrowThis()の戻り値はglobalになっています。ファイル名で実行したとき (node ファイル名.js) の戻り値は
{}に対して、Node REPL ではglobalが返されるのですが、どのような違いがあるのでしょうか。また Node REPL の実行だと
arrowThis() === thisとarrowThis() === globalThisがどちらともtrueですが、ファイル名で実行するとarrowThis() === globalThisがfalseになっています。Beta Was this translation helpful? Give feedback.
All reactions