{"id":508,"date":"2025-12-05T15:27:53","date_gmt":"2025-12-05T07:27:53","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=508"},"modified":"2025-12-05T15:27:53","modified_gmt":"2025-12-05T07:27:53","slug":"javascript%e6%b2%99%e7%ae%b1%e9%9a%94%e7%a6%bb%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript%e6%b2%99%e7%ae%b1%e9%9a%94%e7%a6%bb%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81\/","title":{"rendered":"JavaScript\u6c99\u7bb1\u9694\u79bb\u793a\u4f8b\u4ee3\u7801"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>JavaScript \u6c99\u7bb1\u9694\u79bb\uff08Sandbox\uff09\u5e38\u7528\u5b9e\u73b0\u65b9\u5f0f + \u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/strong>\uff0c\u5305\u542b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u57fa\u4e8e <code>new Function<\/code> \u7684\u57fa\u672c\u6c99\u7bb1<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>Proxy<\/code> \u7684\u589e\u5f3a\u6c99\u7bb1<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>iframe<\/code> \u7684\u6d4f\u89c8\u5668\u6c99\u7bb1<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>VM2<\/code> \u7684 Node.js \u6c99\u7bb1<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>Realm<\/code> \/ <code>ShadowRealm<\/code> \u7684\u73b0\u4ee3\u6c99\u7bb1\u793a\u4f8b<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u51e0\u79cd\u65b9\u5f0f\u8986\u76d6\u6d4f\u89c8\u5668\u3001Node.js\uff0c\u4ee5\u53ca\u5b9e\u9645\u5de5\u7a0b\u5e38\u7528\u7684\u6c99\u7bb1\u6280\u672f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 1. \u57fa\u4e8e <code>new Function<\/code> \u7684\u57fa\u7840\u6c99\u7bb1\uff08\u6700\u7b80\u5355\uff09<\/h1>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u901a\u8fc7 <strong>\u9650\u5236\u5916\u90e8\u53d8\u91cf\u8bbf\u95ee<\/strong> \u6765\u5b9e\u73b0\u7b80\u5355\u6c99\u7bb1\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction createSandbox(code) {\n  return new Function(&quot;sandbox&quot;, `\n    with (sandbox) {\n      return eval(\\`${code}\\`)\n    }\n  `);\n}\n\nconst sandbox = { console };\n\nconst run = createSandbox(&quot;console.log(&#039;\u6c99\u7bb1\u8fd0\u884c\u6210\u529f\uff01&#039;); 1 + 2&quot;);\nconsole.log(run(sandbox)); \/\/ \u8f93\u51fa\uff1a3\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f \u7f3a\u70b9\uff1a<br><code>with + eval<\/code> \u5bb9\u6613\u88ab\u9003\u9038\uff0c\u4e0d\u7b97\u771f\u6b63\u5b89\u5168\uff0c\u53ea\u9002\u5408\u57fa\u7840\u9694\u79bb\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 2. Proxy + with \u5b8c\u6574\u6c99\u7bb1\uff08\u524d\u7aef\u6846\u67b6\u5e38\u7528\uff0c\u5982 Qiankun\uff09<\/h1>\n\n\n\n<p>\u8fd9\u662f\u524d\u7aef\u5e38\u7528\u6280\u672f\uff0c\u7528 Proxy \u6a21\u62df\u5168\u5c40\u5bf9\u8c61\uff0c\u963b\u65ad window \u6c61\u67d3\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction createProxySandbox() {\n  const fakeWindow = {};\n  return new Proxy(fakeWindow, {\n    get(target, key) {\n      if (key === &quot;window&quot;) return fakeWindow;\n      return target&#x5B;key] ?? window&#x5B;key];\n    },\n    set(target, key, value) {\n      target&#x5B;key] = value; \/\/ \u9650\u5236\u6c61\u67d3 window\n      return true;\n    }\n  });\n}\n\nconst sandbox = createProxySandbox();\n\n(function (window) {\n  window.test = &quot;from sandbox&quot;;  \n  console.log(window.test);\n})(sandbox);\n\nconsole.log(window.test); \/\/ undefined\uff1a\u672a\u6c61\u67d3\u5168\u5c40\n\n<\/pre><\/div>\n\n\n<p>\u25b6 \u5b9e\u6218\u7528\u9014\uff1a<br><strong>\u5fae\u524d\u7aef\u6846\u67b6<\/strong>\uff08\u5982 qiankun\u3001single-spa\uff09\u5927\u91cf\u4f7f\u7528\u8fd9\u79cd\u6c99\u7bb1\u65b9\u6cd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 3. \u4f7f\u7528 iframe \u6c99\u7bb1\uff08\u6d4f\u89c8\u5668\u6700\u5b89\u5168\u529e\u6cd5\u4e4b\u4e00\uff09<\/h1>\n\n\n\n<p>\u6d4f\u89c8\u5668\u4e2d\u7684 <strong>\u6700\u4f73\u9694\u79bb\u65b9\u5f0f\u5c31\u662f iframe<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HTML<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;iframe id=&quot;sandbox&quot; sandbox=&quot;allow-scripts&quot;&gt;&amp;lt;\/iframe&gt;\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">JS<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst iframe = document.getElementById(&#039;sandbox&#039;);\n\n\/\/ \u6267\u884c\u6c99\u7bb1\u4ee3\u7801\niframe.contentWindow.eval(`\n  console.log(&#039;Iframe sandbox works!&#039;);\n  window.a = 123;\n`);\n\nconsole.log(window.a); \/\/ undefined\n\n<\/pre><\/div>\n\n\n<p>\u25b6 \u4f18\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b8c\u5168\u9694\u79bb\u4f5c\u7528\u57df<\/li>\n\n\n\n<li>\u5b89\u5168\u6027\u6700\u5f3a\uff08\u6d4f\u89c8\u5668\u9694\u79bb\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 4. Node.js \u4e2d\u4f7f\u7528 <code>vm<\/code> \u6a21\u5757\u6c99\u7bb1<\/h1>\n\n\n\n<p>Node \u5b98\u65b9\u5185\u7f6e\u7684\u6c99\u7bb1\u6a21\u5757\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst vm = require(&quot;vm&quot;);\n\nconst sandbox = { counter: 0 };\nvm.createContext(sandbox);\n\nconst code = `\n  counter += 1;\n  &quot;\u6267\u884c\u7ed3\u679c\uff1a&quot; + counter;\n`;\n\nconst result = vm.runInContext(code, sandbox);\n\nconsole.log(result); \/\/ \u6267\u884c\u7ed3\u679c\uff1a1\nconsole.log(sandbox.counter); \/\/ 1\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f \u6ce8\u610f\uff1a<br>Node \u5185\u7f6e <code>vm<\/code> \u4e0d\u662f\u5b8c\u5168\u5b89\u5168\uff0c\u63a8\u8350\u4f7f\u7528 vm2\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 5. \u4f7f\u7528 VM2\uff08Node.js \u751f\u4ea7\u7ea7\u6c99\u7bb1\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst { VM } = require(&#039;vm2&#039;);\n\nconst vm = new VM({\n  timeout: 1000,\n  sandbox: { x: 1 }\n});\n\nconst result = vm.run(`\n  x = x + 5;\n  x;\n`);\n\nconsole.log(result); \/\/ 6\n\n<\/pre><\/div>\n\n\n<p>VM2 \u662f Node \u91cc\u6700\u5b89\u5168\u7684\u6c99\u7bb1\u4e4b\u4e00\uff08\u4f46\u5386\u53f2\u4e0a\u4e5f\u6709\u6f0f\u6d1e\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 6. \u4f7f\u7528 ShadowRealm\uff08\u65b0\u6807\u51c6\uff0c\u771f\u6b63\u9694\u79bb\u751f\u6001\uff09<\/h1>\n\n\n\n<p>\u90e8\u5206\u73af\u5883\u5df2\u652f\u6301 ShadowRealm\uff08\u66f4\u5b89\u5168\u7684 ES \u9694\u79bb\uff09\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst realm = new ShadowRealm();\n\nrealm.evaluate(`\n  globalThis.value = 100;\n  console.log(&quot;\u6765\u81ea ShadowRealm\uff1a&quot;, value);\n`);\n\n<\/pre><\/div>\n\n\n<p>ShadowRealm \u7684\u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b8c\u5168\u9694\u79bb\u4f5c\u7528\u57df<\/li>\n\n\n\n<li>\u6ca1\u6709\u5171\u4eab window \/ globalThis<\/li>\n\n\n\n<li>\u539f\u751f\u5b89\u5168<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 \u5bf9\u6bd4\u603b\u7ed3\u8868<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6280\u672f\u65b9\u6848<\/th><th>\u73af\u5883<\/th><th>\u5b89\u5168\u6027<\/th><th>\u9002\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td>new Function + with<\/td><td>JS \u5168\u73af\u5883<\/td><td>\u274c\u5f31<\/td><td>Demo\u3001\u7b80\u5355\u9694\u79bb<\/td><\/tr><tr><td>Proxy \u6c99\u7bb1<\/td><td>\u6d4f\u89c8\u5668<\/td><td>\u2b50\u2b50<\/td><td>\u5fae\u524d\u7aef\u3001\u63d2\u4ef6\u7cfb\u7edf<\/td><\/tr><tr><td>iframe<\/td><td>\u6d4f\u89c8\u5668<\/td><td>\u2b50\u2b50\u2b50\u2b50\u2b50<\/td><td>\u6700\u5b89\u5168\u6d4f\u89c8\u5668\u9694\u79bb<\/td><\/tr><tr><td>Node vm<\/td><td>Node<\/td><td>\u2b50\u2b50<\/td><td>\u7b80\u5355\u6c99\u7bb1<\/td><\/tr><tr><td>VM2<\/td><td>Node<\/td><td>\u2b50\u2b50\u2b50\u2b50<\/td><td>\u670d\u52a1\u5668\u7aef\u5b89\u5168\u6267\u884c\u4ee3\u7801<\/td><\/tr><tr><td>ShadowRealm<\/td><td>\u652f\u6301\u73af\u5883\u5c11<\/td><td>\u2b50\u2b50\u2b50\u2b50<\/td><td>\u672a\u6765\u8d8b\u52bf\u3001\u5b8c\u7f8e\u9694\u79bb<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd JavaScript \u6c99\u7bb1\u9694\u79bb\uff08Sandbox\uff09\u5e38\u7528\u5b9e\u73b0\u65b9\u5f0f &#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript%e6%b2%99%e7%ae%b1%e9%9a%94%e7%a6%bb%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/508\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}