{"id":86377,"date":"2020-05-15T12:24:20","date_gmt":"2020-05-15T04:24:20","guid":{"rendered":"http:\/\/4563.org\/?p=86377"},"modified":"2020-05-15T12:24:20","modified_gmt":"2020-05-15T04:24:20","slug":"%e7%94%a8-node-%e6%89%a7%e8%a1%8c-js-%e6%96%87%e4%bb%b6%e7%9a%84%e7%bb%93%e6%9e%9c%e5%92%8c-node-interpreter-%e9%87%8c%e8%bf%90%e8%a1%8c%e7%9a%84%e7%bb%93%e6%9e%9c%e4%b8%8d%e4%b8%80%e6%a0%b7","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=86377","title":{"rendered":"\u7528 node \u6267\u884c js \u6587\u4ef6\u7684\u7ed3\u679c\u548c node interpreter \u91cc\u8fd0\u884c\u7684\u7ed3\u679c\u4e0d\u4e00\u6837"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7528 node \u6267\u884c js \u6587\u4ef6\u7684\u7ed3\u679c\u548c node interpreter \u91cc\u8fd0\u884c\u7684\u7ed3\u679c\u4e0d\u4e00\u6837               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lbfeng <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>Node.js v14.1.0<\/p>\n<pre><code>var x = 'x'; console.log(this) \/\/ empty object or global object  function foo() {   console.log(this.x); }  var obj = {   x: 'asdf',   f: foo }  obj.f()  var func = obj.f; func(); <\/code><\/pre>\n<p>\u5982\u679c\u7528<code>node test.js<\/code>\u7b2c\u4e00\u4e2a this \u6307\u5411{}, \u5728 foo \u91cc this \u7b2c\u4e00\u6b21\u6307\u5411 obj,\u7b2c\u4e8c\u6b21\u6307\u5411 global<\/p>\n<p>\u7528<code>node<\/code>\u8fdb\u5165\u7f16\u8f91\u5668\u7b2c\u4e00\u4e2a this \u6307\u5411 global, \u5728 foo \u91cc this \u7b2c\u4e00\u6b21\u6307\u5411 obj,\u7b2c\u4e8c\u6b21\u6307\u5411 global<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1491626\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : noe132 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             node \u6267\u884c\u7684\u65f6\u5019\u662f\u6a21\u5757\u3002this \u662f\u6a21\u5757\u7684 global                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1491627\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rabbbit <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             module.exports.a = 1<br \/>console.log(this) \/\/ { a: 1 }                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1491628\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : lbfeng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u4eba\u77e5\u9053\u4e3a\u5565\u201c\u5982\u679c\u7528 node test.js \u7b2c\u4e00\u4e2a this \u6307\u5411{}, \u5728 foo \u91cc this \u7b2c\u4e00\u6b21\u6307\u5411 obj,\u7b2c\u4e8c\u6b21\u6307\u5411 global\u201d\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1491629\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9996\u5148\uff0cNode \u7684 REPL \u73af\u5883\u548c\u6b63\u5e38\u6267\u884c\u73af\u5883\u662f\u4e0d\u540c\u7684\u4e24\u4e2a\u73af\u5883\uff0c\u800c this \u662f\u548c\u73af\u5883\u76f8\u5173\u7684\uff0c\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u8c03\u7528 this \u53ef\u80fd\u4f1a\u5bfc\u81f4\u8fd4\u56de\u4e0d\u540c\u7684\u7ed3\u679c\uff0c\u8fd9\u4e2a\u5efa\u8bae\u770b\u4e00\u4e0b Node \u7684\u6587\u6863\u6bd4\u8f83\u4e00\u4e0b\u3002<\/p>\n<p>\u7136\u540e\u5c31\u662f\u5206\u6790\u4e24\u79cd\u73af\u5883\u4e0b\u90fd\u5e94\u8be5\u8f93\u51fa\u4ec0\u4e48\u503c\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<br \/>REPL \u73af\u5883\uff08\u76f4\u63a5\u542f\u52a8 Node \u8fdb\u5165\u4ea4\u4e92\u754c\u9762\u8f93\u5165\u6307\u4ee4\uff09\uff1a<br \/>var x = &#8216;x&#8217;; \/\/ REPL \u73af\u5883\u4e0b\u7528 var \u58f0\u660e\u76f8\u5f53\u4e8e\u5728 global \u4e0a\u58f0\u660e\uff08\u4f60\u7528 let \u548c const \u5c31\u4e0d\u4e00\u6837\u4e86\uff09\uff0c\u6240\u4ee5\u8fd9\u53e5\u662f\u5728 global \u4e0b\u58f0\u660e\u4e86 x\uff0c\u5e76\u8d4b\u503c&#8217;x&#8217;\u3002<br \/>console.log(this) \/\/ \u8f93\u51fa\u7684\u662f global \u5bf9\u8c61\u3002<\/p>\n<p>function foo() {<br \/> console.log(this.x);<br \/>}<\/p>\n<p>\/\/ \u548c\u4e0a\u9762 x \u7684\u5b9a\u4e49\u4e00\u6837\uff0c\u8fd9\u91cc\u5728 global \u4e0a\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5c5e\u6027 obj\uff0cobj \u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u5bf9\u8c61\uff1a<br \/>\/\/ \u6709\u4e00\u4e2a\u5c5e\u6027 x\uff0c\u503c\u4e3a&#8217;asdf&#8217;\uff1b<br \/>\/\/ \u6709\u4e00\u4e2a\u65b9\u6cd5 f\uff0c\u5f15\u7528\u7684 foo \u51fd\u6570\u3002<br \/>var obj = {<br \/> x: &#8216;asdf&#8217;,<br \/> f: foo<br \/>}<\/p>\n<p>obj.f(); \/\/ \u8c03\u7528 obj \u4e0b\u7684 f \u65b9\u6cd5\uff0c\u5b9e\u9645\u4e0a\u662f\u8ba9 foo \u51fd\u6570\u5728 obj \u4f5c\u4e3a this \u7684\u524d\u63d0\u4e0b\u8fd0\u884c\uff0c\u8f93\u51fa\u7684 this.x \u80af\u5b9a\u5c31\u662f obj.x \u3002<\/p>\n<p>var func = obj.f; \/\/ \u8fd9\u91cc\u662f\u628a obj.f \u7684\u5f15\u7528\u8d4b\u503c\u7ed9 func\uff0c\u800c obj.f \u7684\u5f15\u7528\u503c\u662f foo\uff0c\u76f8\u5f53\u4e8e `var func = foo;` \u6b64\u65f6 func \u4e0e obj \u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002<\/p>\n<p>func(); \/\/ \u56e0\u4e3a func \u662f\u76f4\u63a5\u5f15\u7528 foo \u7684\uff0c\u76f8\u5f53\u4e8e foo \u7684\u522b\u540d\uff0c\u6240\u4ee5\u8fd9\u4e00\u884c\u4ee3\u7801\u5b8c\u5168\u7b49\u4ef7\u4e8e`foo();`\uff1b\u800c foo \u662f\u5728 global \u5bf9\u8c61\u4e0b\u6267\u884c\u7684\uff0cthis \u4e5f\u662f\u6307\u5411\u7684 global\uff0c\u6240\u4ee5 this.x \u5c31\u662f&#8217;x&#8217;\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<br \/>\u6b63\u5e38\u6267\u884c\u73af\u5883\uff08 node test.js \uff09\uff1a<\/p>\n<p>\/\/ \u6b63\u5e38\u6267\u884c\u73af\u5883\u4e0b\u4e00\u5207\u7a0b\u5e8f\u6587\u4ef6\u7686\u4e3a module\uff0c\u6240\u4ee5 test.js \u6587\u4ef6\u91cc\u7684\u6240\u6709\u4ee3\u7801\u4e5f\u90fd\u662f\u5728\u4e00\u4e2a module \u73af\u5883\u4e0b\u7684\uff0c\u800c module \u521d\u59cb\u60c5\u51b5\u4e0b\u5c31\u662f\u4e00\u4e2a\u7a7a\u5bf9\u8c61\u3002<\/p>\n<p>var x = &#8216;x&#8217;; \/\/ \u53ea\u662f\u5728\u5f53\u524d module \u4f5c\u7528\u57df\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f46\u5176\u5e76\u4e0d\u662f module \u7684\u5c5e\u6027\uff0c\u6240\u4ee5\u8fd9\u65f6\u5019\u4f60\u7528 this.x \u662f\u65e0\u6cd5\u8f93\u51fa&#8217;x&#8217;\u7684\u3002<br \/>console.log(this); \/\/ module \u76f4\u63a5\u4f5c\u7528\u57df\u4e0b\u7684 this \u5c31\u662f module \u672c\u8eab\uff0c\u8fd9\u65f6\u5019\u8fd8\u6ca1\u6709\u4e3a module \u58f0\u660e\u4efb\u4f55\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u6240\u4ee5\u5c31\u662f\u4e2a\u7a7a\u5bf9\u8c61\u3002<\/p>\n<p>function foo() {<br \/> console.log(this.x);<br \/>}<\/p>\n<p>\/\/ \u8fd9\u91cc\u76f8\u5f53\u4e8e\u662f\u5728 module \u4f5c\u7528\u57df\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cf obj\uff0c\u540c\u6837\uff0c\u8fd9\u4e2a obj \u4e5f\u4e0d\u662f\u8fd9\u4e2a module \u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<br \/>\/\/ obj \u5f15\u7528\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u6709\u4e00\u4e2a\u5c5e\u6027 x\uff0c\u503c\u4e3a&#8217;asdf&#8217;\uff1b\u6709\u4e00\u4e2a\u65b9\u6cd5 f\uff0c\u5f15\u7528\u7684 foo \u51fd\u6570\u3002\u8fd9\u5757\u548c REPL \u4e2d\u7684\u4e00\u6837\u3002<br \/>var obj = {<br \/> x: &#8216;asdf&#8217;,<br \/> f: foo<br \/>}<\/p>\n<p>obj.f(); \/\/ \u548c REPL \u4e2d\u7684\u4e00\u6837\uff0c\u56e0\u4e3a f \u662f obj \u7684\u65b9\u6cd5\uff0c\u6240\u4ee5\u662f\u4ee5 obj \u4e3a this \u6765\u6267\u884c\u7684\uff0c\u8f93\u51fa this.x \u5176\u5b9e\u662f obj.x \u3002<\/p>\n<p>var func = obj.f; \/\/ \u548c REPL \u4e2d\u7684\u4e00\u6837\uff0c\u628a obj.f \u7684\u5f15\u7528\u8d4b\u503c\u7ed9 func\uff0c\u800c obj.f \u7684\u5f15\u7528\u503c\u662f foo\uff0c\u76f8\u5f53\u4e8e `var func = foo;` \u6b64\u65f6 func \u4e0e obj \u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002<br \/>func(); \/\/ \u548c REPL \u4e2d\u7684\u4e00\u6837\uff0c\u56e0\u4e3a func \u662f\u76f4\u63a5\u5f15\u7528 foo \u7684\uff0c\u76f8\u5f53\u4e8e foo \u7684\u522b\u540d\uff0c\u6240\u4ee5\u8fd9\u4e00\u884c\u4ee3\u7801\u5b8c\u5168\u7b49\u4ef7\u4e8e`foo();`\uff1b\u800c foo \u662f\u5728 module \u5bf9\u8c61\u4e0b\u6267\u884c\u7684\uff0cthis \u4e5f\u662f\u6307\u5411\u7684 module\uff0c\u6240\u4ee5 this.x \u5b9e\u9645\u4e0a\u662f\u8bfb\u53d6 module \u7684 x \u5c5e\u6027\u7684\u503c\uff0c\u7136\u800c module.x \u662f undefined \uff08\u524d\u9762\u6240\u6709\u7684\u58f0\u660e\u7684 x \u90fd\u4e0d\u662f module \u7684\u5c5e\u6027\uff09\uff0c\u6240\u4ee5\u5c31\u8f93\u51fa undefined \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 node \u6267\u884c js \u6587\u4ef6\u7684\u7ed3&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/86377"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=86377"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/86377\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=86377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=86377"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=86377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}