{"id":2423,"date":"2020-06-25T23:28:40","date_gmt":"2020-06-25T15:28:40","guid":{"rendered":"http:\/\/www.ponybai.com\/?p=2423"},"modified":"2020-06-25T23:28:40","modified_gmt":"2020-06-25T15:28:40","slug":"salesforce%e5%bc%80%e5%8f%91%e4%ba%ba%e5%91%98%e7%9a%84javascript%e6%8a%80%e8%83%bd-%e4%ba%86%e8%a7%a3%e4%b8%8a%e4%b8%8b%e6%96%87%ef%bc%8c%e8%8c%83%e5%9b%b4%e5%92%8c%e9%97%ad%e5%8c%85","status":"publish","type":"post","link":"http:\/\/www.ponybai.com\/?p=2423","title":{"rendered":"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305"},"content":{"rendered":"\n\t\t\t\t\n<h2 class=\"wp-block-heading\">\u5b66\u4e60\u76ee\u6807<\/h2>\n\n\n\n<p>\u5b8c\u6210\u672c\u5355\u5143\u540e\uff0c\u60a8\u5c06\u80fd\u591f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u786e\u5b9a\u53d8\u91cf\u5728JavaScript\u4e2d\u7684\u8303\u56f4\u3002<\/li><li>\u63cf\u8ff0\u5982\u4f55<code>this<\/code>\u6839\u636e\u8c03\u7528\u51fd\u6570\u7684\u4f4d\u7f6e\u8fdb\u884c\u66f4\u6539\u3002<\/li><li>\u4f7f\u7528\u95ed\u5305\u6355\u83b7\u5bf9\u51fd\u6570\u4e2d\u53d8\u91cf\u7684\u5f15\u7528\u3002<\/li><\/ul>\n\n\n\n<p>\u7406\u89e3\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u7684\u5173\u952e\u5728\u4e8e\u7406\u89e3\u53d8\u91cf\u7684\u53ef\u7528\u6027\uff0c\u5982\u4f55\u7ef4\u62a4\u72b6\u6001\u4ee5\u53ca\u5982\u4f55\u8bbf\u95ee\u8be5\u72b6\u6001\u3002&nbsp;<\/p>\n\n\n\n<p>\u5728JavaScript\u4e2d\uff0c\u53d8\u91cf\u7684\u53ef\u7528\u6027\u548c\u53ef\u89c1\u6027\u79f0\u4e3a\u8303\u56f4\u3002\u8303\u56f4\u7531\u58f0\u660e\u53d8\u91cf\u7684\u4f4d\u7f6e\u786e\u5b9a\u3002&nbsp;<\/p>\n\n\n\n<p>\u4e0a\u4e0b\u6587\u662f\u5f53\u524d\u4ee3\u7801\u6267\u884c\u7684\u72b6\u6001\u3002\u901a\u8fc7<code>this<\/code>\u6307\u9488\u8bbf\u95ee\u5b83\u3002&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/javascript-essentials-salesforce-developers\/context-scope-closures#-----------variable-scope--\"><\/a>\u53ef\u53d8\u8303\u56f4<\/h2>\n\n\n\n<p>\u5728JavaScript\u53d8\u91cf\u4f7f\u7528\u7533\u62a5<code>var<\/code>\uff0c<code>let<\/code>\u6216<code>const<\/code>\u5173\u952e\u5b57\u3002\u5728\u4f55\u5904\u8c03\u7528\u5173\u952e\u5b57\u6307\u793a\u8981\u521b\u5efa\u7684\u53d8\u91cf\u7684\u8303\u56f4\u3002&nbsp;<\/p>\n\n\n\n<p>\u4e86\u89e3\u8fd9\u4e09\u8005\u4e4b\u95f4\u7684\u533a\u522b\u5f52\u7ed3\u4e3a\u4e24\u4e2a\u56e0\u7d20\uff1a\u5206\u914d\u53ef\u53d8\u6027\u548c\u652f\u6301\u975e\u529f\u80fd\u5757\u4f5c\u7528\u57df\u3002\u6211\u4eec\u5728\u672c\u6a21\u5757\u7684\u7b2c\u4e00\u4e2a\u5355\u5143\u4e2d\u4ecb\u7ecd\u4e86\u5206\u914d\u53ef\u53d8\u6027\u3002\u73b0\u5728\u8be5\u8ba8\u8bba\u8303\u56f4\u4e86\u3002&nbsp;<\/p>\n\n\n\n<p><strong>\u8303\u56f4\u7684\u4e50\u8da3<\/strong><\/p>\n\n\n\n<p>\u58f0\u660e\u53d8\u91cf\u6216\u53c2\u6570\u7684\u4ee3\u7801\u5757\u786e\u5b9a\u5176\u8303\u56f4\u3002\u4f46<code>var<\/code>\u4e0d\u80fd\u8bc6\u522b\u975e\u529f\u80fd\u4ee3\u7801\u5757\u3002\u8fd9\u610f\u5473\u7740\u8c03\u7528<code>var<\/code>\u5728\u4e00\u4e2a<code>if<\/code>\u5757\u6216\u4e00\u4e2a\u73af\u5757\u5c06\u53d8\u91cf\u5206\u914d\u7ed9\u6700\u8fd1\u7684\u5c01\u95ed\u529f\u80fd\u7684\u8303\u56f4\u3002\u6b64\u529f\u80fd\u79f0\u4e3a<em>\u63d0\u5347<\/em>\u3002&nbsp;<\/p>\n\n\n\n<p>\u4f7f\u7528<code>let<\/code>\u6216\u65f6<code>const<\/code>\uff0c\u53c2\u6570\u6216\u53d8\u91cf\u7684\u8303\u56f4\u59cb\u7ec8\u662f\u5728\u5176\u4e2d\u58f0\u660e\u53c2\u6570\u7684\u5b9e\u9645\u5757\u3002\u6709\u4e00\u4e2a\u7ecf\u5178\u7684\u601d\u7ef4\u00a0\u7ec3\u4e60\u53ef\u4ee5\u8bc1\u660e\u8fd9\u4e00\u70b9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function countToThree() {\n  \/\/ i is in the scope of the countToThree function\n  for (var i = 0; i &lt; 3; i++){\n    console.log(i); \/\/ iteration 1: 0\n    \/\/ iteration 2: 1\n    \/\/ iteration 3: 2\n  }\n  console.log(i); \/\/ What is this?\n}<\/code><\/pre>\n\n\n\n<p>\u6240\u8ff0<code>console.log<\/code>\u5185\u90e8\u8f93\u51fa<code>for<\/code>\u56de\u8def\u4e0d\u8db3\u4e3a\u5947\uff0c\u8f93\u51fa\u7684\u503c<code>i<\/code>\u5bf9\u4e8e\u6bcf\u6b21\u8fed\u4ee3\u3002\u53ef\u80fd\u66f4\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\u6700\u7ec8<code>console.log<\/code>\u58f0\u660e\uff0c\u5b83\u8f93\u51fa<code>3<\/code>\u3002\u60a8\u53ef\u80fd\u5df2\u7ecf\u9884\u6599\u5230\u4e86\u9519\u8bef\uff0c\u56e0\u4e3a<code>i<\/code>\u5728\u60a8\u8ba4\u4e3a\u662f<code>for<\/code>\u5faa\u73af\u8303\u56f4\u4e4b\u5185\u58f0\u660e\u4e86\u8be5\u9519\u8bef\u3002\u4f46\u662f\u968f\u7740\u540a\u88c5\uff0c<code>i<\/code>\u5b9e\u9645\u4e0a\u5c5e\u4e8e<code>countToThree<\/code>\u7684\u8303\u56f4\u3002&nbsp;<\/p>\n\n\n\n<p>\u867d\u7136\u4e0d\u4e00\u5b9a\u5f88\u7cdf\u7cd5\uff0c\u4f46\u662f\u5982\u679c\u5728\u4ee3\u7801\u5757\u4e2d\u91cd\u65b0\u58f0\u660e\u4e86\u53d8\u91cf\uff0c\u5219\u540a\u88c5\u901a\u5e38\u4f1a\u88ab\u8bef\u89e3\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u53d8\u91cf\u6cc4\u6f0f\u6216\u5bfc\u81f4\u610f\u5916\u8986\u76d6\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u8bef\u89e3<code>let<\/code>\uff0c\u5c06<code>const<\/code>\u5176\u6dfb\u52a0\u5230\u8bed\u8a00\u4e2d\u4ee5\u521b\u5efa\u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u7684\u53d8\u91cf\u3002\u8ba9\u6211\u4eec\u91cd\u65b0\u601d\u8003\u4e00\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (let j = 0; j &lt; 3; j++){\n  console.log(j); \/\/ 0\n  \/\/ 1\n  \/\/ 2\n}\nconsole.log(j); \/\/ error<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u66ff\u6362<code>let<\/code>\u4e3a<code>var<\/code>\uff0c\u6211\u4eec\u73b0\u5728\u6709\u4e86\u4e00\u4e2a\u4ec5\u5728<code>for<\/code>\u5faa\u73af\u4e0a\u4e0b\u6587\u4e2d\u5b58\u5728\u7684\u53d8\u91cf\u3002\u5728\u5faa\u73af\u5173\u95ed\u540e\u5c1d\u8bd5\u8bbf\u95ee\u5b83\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u9519\u8bef\u3002&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/javascript-essentials-salesforce-developers\/context-scope-closures#-----------context-and-this--\"><\/a>\u4e0a\u4e0b\u6587\u548c\u8fd9\u4e2a<\/h2>\n\n\n\n<p>\u6b63\u5982\u6211\u4eec\u63a2\u7d22\u7684\u90a3\u6837\uff0cJavaScript\u56f4\u7ed5\u5bf9\u8c61\u3002\u5bf9\u8c61\u662f\u8ddf\u8e2a\u72b6\u6001\u7684\u5730\u65b9\u3002\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u8be5\u51fd\u6570\u5468\u56f4\u59cb\u7ec8\u6709\u4e00\u4e2a\u5bf9\u8c61\u5bb9\u5668\u3002\u8be5\u5bf9\u8c61\u5bb9\u5668\u662f\u5176\u4e0a\u4e0b\u6587\uff0c<code>this<\/code>\u5173\u952e\u5b57\u6307\u5411\u8be5\u4e0a\u4e0b\u6587\u3002\u56e0\u6b64\uff0c\u5728<em>\u58f0\u660e<\/em>\u51fd\u6570\u65f6\u4e0d\u4f1a\u8bbe\u7f6e\u4e0a\u4e0b\u6587\uff0c\u800c\u662f\u5728<em>\u8c03\u7528<\/em>\u51fd\u6570\u65f6\u8bbe\u7f6e\u4e0a\u4e0b\u6587\u3002&nbsp;<\/p>\n\n\n\n<p>\u56e0\u4e3a\u529f\u80fd\u53ef\u4ee5\u5728\u5bf9\u8c61\u4e4b\u95f4\u4f20\u9012\uff0c\u6240\u4ee5<code>this<\/code>\u6307\u5411\u7684\u5185\u5bb9\u53ef\u4ee5\u66f4\u6539\u3002&nbsp;<\/p>\n\n\n\n<p>\u4f8b\u5982\u8bf4\u8fd9\u4e2aJavaScript\u00a0\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {\n  aValue: 0,\n  increment: function(incrementBy) {\n    this.aValue = this.aValue + incrementBy;\n  }\n}<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u7136\u540e\u8bbf\u95ee\u589e\u91cf\u529f\u80fd\uff0c\u5b83\u5c06\u6309\u9884\u671f\u5de5\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>obj.increment(2);\nconsole.log(obj.aValue); \/\/ 2<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\uff0c\u8ba9\u6211\u4eec\u5c06\u8be5\u51fd\u6570\u5206\u914d\u7ed9\u53e6\u4e00\u4e2a\u53d8\u91cf\uff0c\u770b\u770b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/assign function to variable\nvar newIncrement = obj.increment;\n\/\/now invoke through the new pointer\nnewIncrement(2);\nconsole.log(obj.aValue); \/\/ still 2 not 4<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u5c06\u53d8\u91cf\u5206\u914d\u7ed9<code>newIncrement<\/code>\uff0c\u73b0\u5728\u53ef\u4ee5\u5728\u5176\u4ed6\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u8be5\u529f\u80fd\u3002\u5177\u4f53\u800c\u8a00\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5728\u5168\u5c40\u8303\u56f4\u5185\u3002&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/hy4kyit2a\/image\/upload\/doc\/trailhead\/en-usb473bb5ea1b7e61dfb07e6a7e547de6b.gif\" alt=\"\u6ce8\u610f\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/javascript-essentials-salesforce-developers\/context-scope-closures#note\"><\/a>\u6ce8\u610f<\/h4>\n\n\n\n<p>\u7684<code>Function.apply()<\/code>\uff0c<code>Function.call()<\/code>\u548c<code>Function.bind()<\/code>\u51fd\u6570\u63d0\u4f9b\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u51fd\u6570\uff0c\u540c\u65f6\u660e\u786e\u5176\u7ed1\u5b9a\u5230\u4e0d\u540c\u7684\u5bf9\u8c61\u4e0a\u4e0b\u6587\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/javascript-essentials-salesforce-developers\/context-scope-closures#-----------the-global-object--\"><\/a>\u5168\u5c40\u5bf9\u8c61<\/h2>\n\n\n\n<p>\u5f53\u6267\u884cJavaScript\u800c\u6ca1\u6709\u4ee5\u5f00\u53d1\u4eba\u5458\u8eab\u4efd\u7f16\u5199\u7684\u4efb\u4f55\u5305\u542b\u5bf9\u8c61\u65f6\uff0c\u5b83\u5c06\u5728\u5168\u5c40\u5bf9\u8c61\u4e2d\u8fd0\u884c\u3002\u56e0\u6b64\uff0c\u636e\u79f0\u5728\u6b64\u8c03\u7528\u7684\u51fd\u6570\u6b63\u5728\u5168\u5c40\u4e0a\u4e0b\u6587\u4e2d\u8fd0\u884c\uff0c\u8fd9\u610f\u5473\u7740\u8bbf\u95ee<code>this<\/code>\u5c06\u6307\u5411\u8be5\u5168\u5c40\u4e0a\u4e0b\u6587\u3002&nbsp;<\/p>\n\n\n\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u5168\u5c40\u4e0a\u4e0b\u6587\u662f<code>window<\/code>\u5bf9\u8c61\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u6d4f\u89c8\u5668\u5f00\u53d1\u4eba\u5458\u5de5\u5177\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u8f7b\u677e\u6d4b\u8bd5\u8be5\u529f\u80fd\u3002&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this === window; \/\/ true<\/code><\/pre>\n\n\n\n<p>\u5728\u8be5<code>increment<\/code>\u793a\u4f8b\u4e2d\uff0c\u5c06<code>increment<\/code>\u51fd\u6570\u5206\u914d\u7ed9<code>newIncrement<\/code>\u53d8\u91cf\u4f1a\u5c06\u8c03\u7528\u5b83\u7684\u4e0a\u4e0b\u6587\u79fb\u52a8\u5230\u5168\u5c40\u5bf9\u8c61\u3002\u8fd9\u5f88\u5bb9\u6613\u8bc1\u660e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(this.aValue); \/\/ NaN\nconsole.log(window.aValue); \/\/ NaN\nconsole.log(typeof window.aValue); \/\/ number<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u5c1d\u8bd5<code>this.aValue<\/code>\u4f7f\u7528\u65b0\u7684\u4e0a\u4e0b\u6587\u8fdb\u884c\u5206\u914d\u65f6\uff0cJavaScript\u5bf9\u8c61\u7684\u53ef\u53d8\u6027\u5f00\u59cb\u53d1\u6325\u4f5c\u7528\u3002\u65b0\u7684\u672a\u521d\u59cb\u5316<code>aValue<\/code>\u5c5e\u6027\u5df2\u6dfb\u52a0\u5230\u4e2d<code>this<\/code>\u3002\u5bf9\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u6267\u884c\u6570\u5b66\u8fd0\u7b97\u5c06\u5931\u8d25\uff0c\u56e0\u6b64\u8be5<code>NaN<\/code>\u503c\u4e5f\u4f1a\u5931\u8d25\u3002\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u770b\u5230<code>aValue<\/code>\u5b58\u5728\u4e8e<code>window<\/code>\uff0c\u7684\u786e\u662f\u4e00\u4e2a\u6570\u5b57\u3002&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/javascript-essentials-salesforce-developers\/context-scope-closures#-----------context-with-your-objects--\"><\/a>\u4e0e\u5bf9\u8c61\u7684\u4e0a\u4e0b\u6587<\/h2>\n\n\n\n<p>\u5728\u6b64<code>increment<\/code>\u793a\u4f8b\u4e2d\uff0c\u53ea\u8981<code>increment<\/code>\u4f7f\u7528<code>obj<\/code>\u70b9\u7b26\u53f7\u6765\u8c03\u7528\u51fd\u6570\uff0c\u5c31<code>this<\/code>\u6307\u5411<code>obj<\/code>\u3002\u6216\u8005\uff0c\u901a\u5e38\u6765\u8bf4\uff0c\u5f53\u8c03\u7528\u51fd\u6570\u4f5c\u4e3a<code>object.function()<\/code>&nbsp;\u70b9\u5de6\u4fa7\u7684\u4e8b\u7269\u65f6\uff0c\u59cb\u7ec8\u662f\u8c03\u7528\u8be5\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u3002&nbsp;<\/p>\n\n\n\n<p>\u60f3\u60f3\u8fd9\u4e2a<code>Bike<\/code>\u4f8b\u5b50\u3002\u7684<code>Bike<\/code>\u6784\u9020\u9650\u5b9a\u4e86\u4e0e\u51e0\u4e2a\u5c5e\u6027<code>this<\/code>\u53c2\u8003\u3002\u5b83\u8fd8\u5177\u6709\u5206\u914d\u7ed9\u5176\u5f15\u7528\u8be5\u539f\u578b\u7684\u529f\u80fd<code>this<\/code>\u3002&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Bike = function(frontIndex, rearIndex){\n  this.frontGearIndex = frontIndex || 0;\n  this.rearGearIndex = rearIndex || 0;\n  ...\n}\n...\nBike.prototype.calculateGearRatio = function(){\n  let front = this.transmission.frontGearTeeth[this.frontGearIndex],\n  rear = this.transmission.rearGearTeeth[this.rearGearIndex];\n  if (front &amp;&amp; rear) {\n    return (front \/ rear) ;\n  } else {\n    return 0;\n  }\n};<\/code><\/pre>\n\n\n\n<p>\u7136\u540e<code>Bike<\/code>\uff0c\u6211\u4eec\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u8c03\u7528\u3002&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const bike = new Bike(1,2);\nconsole.log(bike.frontGearIndex); \/\/ 1\nconsole.log(bike.rearGearIndex); \/\/ 2<\/code><\/pre>\n\n\n\n<p>\u770b\u8d77\u6765\u6211\u4eec\u6b63\u5728<code>Bike<\/code>\u5168\u5c40\u4e0a\u4e0b\u6587\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\u3002\u4f46\u662f\uff0c<code>new<\/code>\u5173\u952e\u5b57\u5c06\u4e0a\u4e0b\u6587\uff08\u548c<code>this<\/code>\u6307\u9488\uff09\u79fb\u52a8\u5230\u5206\u914d\u5de6\u4fa7\u7684\u65b0\u5bf9\u8c61\u3002&nbsp;<\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u8c03\u7528\u4efb\u4f55\u51fd\u6570\u65f6\uff0c\u5b83\u4eec\u73b0\u5728\u662f<code>bike<\/code>\u5bf9\u8c61\u7684\u6210\u5458\uff0c\u56e0\u6b64\u5b83\u4eec\u5c06\u5176\u7528\u4f5c\u5305\u542b\u4e0a\u4e0b\u6587\u3002&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let gearRatio = bike.calculateGearRatio();\nconsole.log(gearRatio); \/\/ 3<\/code><\/pre>\n\n\n\n<p>\u4ee5\u9519\u8bef\u7684\u65b9\u5f0f\u8c03\u7528\u6784\u9020\u51fd\u6570\u5f88\u5bb9\u6613\u3002\u5728\u8fd9\u91cc\u4e8b\u60c5\u4f1a\u5d29\u6e83\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const badBike = Bike(1,2);\nconsole.log(badBike.frontGearIndex); \/\/ error\nconsole.log(window.frontGearIndex); \/\/ 1<\/code><\/pre>\n\n\n\n<p>\u5f53\u60a8\u5fd8\u8bb0\u4f7f\u7528\u65f6<code>new<\/code>\uff0c<code>Bike<\/code>\u5c06\u50cf\u5176\u4ed6\u4efb\u4f55\u51fd\u6570\u4e00\u6837\u8c03\u7528\uff0c\u5e76\u4e14<code>this<\/code>\u4ece<code>window<\/code>\u5230\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u5173\u952e\u8f6c\u6362\u5931\u8d25\u3002<code>frontGearIndex<\/code>&nbsp;\u5f15\u5165\u5bf9\u8c61\u53ef\u53d8\u6027\uff0c\u5e76\u6dfb\u52a0\u5c5e\u6027<code>window<\/code>\u3002&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/javascript-essentials-salesforce-developers\/context-scope-closures#note-1\"><\/a>\u6ce8\u610f<\/h4>\n\n\n\n<p><code>class<\/code>JavaScript\u4e2d\u7684\u8bed\u6cd5\u4f1a\u5f3a\u5236\u60a8\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u56e0\u6b64\u60a8\u4e0d\u4f1a\u8bef\u5bfc\u4e0a\u4e0b\u6587\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/javascript-essentials-salesforce-developers\/context-scope-closures#-----------closures--\"><\/a>\u5173\u95ed<\/h2>\n\n\n\n<p>\u58f0\u660e\u51fd\u6570\u65f6\uff0c\u5b83\u5c06\u4fdd\u7559\u5bf9\u5176\u4e2d\u58f0\u660e\u7684\u4efb\u4f55\u53d8\u91cf\u6216\u53c2\u6570\u7684\u5f15\u7528\uff0c\u4ee5\u53ca\u5bf9\u5176\u6240\u5305\u542b\u8303\u56f4\u5185\u5f15\u7528\u7684\u4efb\u4f55\u53d8\u91cf\u7684\u5f15\u7528\u3002\u5b83\u7684\u53d8\u91cf\u548c\u81ea\u53d8\u91cf\u4ee5\u53ca\u5176\u5305\u542b\u8303\u56f4\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u548c\u81ea\u53d8\u91cf\u7684\u8fd9\u79cd\u7ec4\u5408\u79f0\u4e3a\u95ed\u5305\u3002&nbsp;<\/p>\n\n\n\n<p>\u8003\u8651\u6b64\u00a0\u51fd\u6570\u53ca\u5176\u8fd4\u56de\u7684\u51fd\u6570\u3002\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const greetingMaker = function(greeting){\n  return function(whoGreeting){\n    return greeting + \", \" + whoGreeting + \"!\";\n  }\n}\nconst greetingHello = greetingMaker(\"Hello\");\nconst greetingBonjour = greetingMaker(\"Bonjour\");\nconst greetingCiao = greetingMaker(\"Ciao\");\nconsole.log(greetingHello(\"Gemma\")); \/\/ Hello, Gemma!\nconsole.log(greetingBonjour(\"Fabien\")); \/\/ Bonjour, Fabien!\nconsole.log(greetingCiao(\"Emanuela\")); \/\/ Ciao, Emanuela!<\/code><\/pre>\n\n\n\n<p>\u5f53<code>greetingMaker<\/code>\u88ab\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u53ef\u4ee5\u60f3\u8c61\u5b83\u7684<code>greeting<\/code>\u53c2\u6570\u4ec5\u5728\u88ab\u8c03\u7528\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u5185\u6301\u7eed\u5b58\u5728\u3002&nbsp;<\/p>\n\n\n\n<p>\u4f46\u662f\u8fd4\u56de\u7684\u51fd\u6570\u4f1a<code>greeting<\/code>\u5728<code>greetingMaker<\/code>\u7684\u8303\u56f4\u5185\u4fdd\u7559\u5bf9\u53c2\u6570\u7684\u5f15\u7528\u3002\u8fd9\u6837\uff0c\u6700\u540e\u901a\u8fc7<code>greetingHello<\/code>\/&nbsp;<code>Bonjour<\/code>\/&nbsp;\u8c03\u7528\u5b83\u65f6<code>Ciao<\/code>\uff0c\u5b83\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u3002&nbsp;<\/p>\n\n\n\n<p>\u638c\u63e1\u95ed\u5305\u4e5f\u662f\u7406\u89e3\u548c\u4f7f\u7528\u8be5\u8bed\u8a00\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002<\/p>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60\u76ee\u6807 \u5b8c\u6210\u672c\u5355\u5143\u540e\uff0c\u60a8\u5c06\u80fd\u591f\uff1a \u786e\u5b9a\u53d8\u91cf\u5728JavaScript\u4e2d\u7684\u8303\u56f4\u3002 \u63cf\u8ff0\u5982\u4f55this\u6839\u636e\u8c03\u7528\u51fd\u6570\u7684\u4f4d &hellip; <a href=\"http:\/\/www.ponybai.com\/?p=2423\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[],"class_list":["post-2423","post","type-post","status-publish","format-standard","hentry","category-lwcjavascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305 - \u767d\u767d\u968f\u7b14<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.ponybai.com\/?p=2423\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305 - \u767d\u767d\u968f\u7b14\" \/>\n<meta property=\"og:description\" content=\"\u5b66\u4e60\u76ee\u6807 \u5b8c\u6210\u672c\u5355\u5143\u540e\uff0c\u60a8\u5c06\u80fd\u591f\uff1a \u786e\u5b9a\u53d8\u91cf\u5728JavaScript\u4e2d\u7684\u8303\u56f4\u3002 \u63cf\u8ff0\u5982\u4f55this\u6839\u636e\u8c03\u7528\u51fd\u6570\u7684\u4f4d &hellip; \u7ee7\u7eed\u9605\u8bfbSalesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.ponybai.com\/?p=2423\" \/>\n<meta property=\"og:site_name\" content=\"\u767d\u767d\u968f\u7b14\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-25T15:28:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/hy4kyit2a\/image\/upload\/doc\/trailhead\/en-usb473bb5ea1b7e61dfb07e6a7e547de6b.gif\" \/>\n<meta name=\"author\" content=\"ponybai\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"ponybai\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/www.ponybai.com\/?p=2423#article\",\"isPartOf\":{\"@id\":\"http:\/\/www.ponybai.com\/?p=2423\"},\"author\":{\"name\":\"ponybai\",\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\"},\"headline\":\"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305\",\"datePublished\":\"2020-06-25T15:28:40+00:00\",\"dateModified\":\"2020-06-25T15:28:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/www.ponybai.com\/?p=2423\"},\"wordCount\":39,\"publisher\":{\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\"},\"articleSection\":[\"LWC\u4e2d\u7684Javascript\"],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.ponybai.com\/?p=2423\",\"url\":\"http:\/\/www.ponybai.com\/?p=2423\",\"name\":\"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305 - \u767d\u767d\u968f\u7b14\",\"isPartOf\":{\"@id\":\"http:\/\/www.ponybai.com\/#website\"},\"datePublished\":\"2020-06-25T15:28:40+00:00\",\"dateModified\":\"2020-06-25T15:28:40+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.ponybai.com\/?p=2423#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.ponybai.com\/?p=2423\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.ponybai.com\/?p=2423#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/www.ponybai.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.ponybai.com\/#website\",\"url\":\"http:\/\/www.ponybai.com\/\",\"name\":\"\u767d\u767d\u968f\u7b14\",\"description\":\"Salesforce\u5b98\u65b9\u8bb2\u5e08\",\"publisher\":{\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.ponybai.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\",\"name\":\"ponybai\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg\",\"contentUrl\":\"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg\",\"width\":248,\"height\":248,\"caption\":\"ponybai\"},\"logo\":{\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/121.37.188.161\"],\"url\":\"http:\/\/www.ponybai.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305 - \u767d\u767d\u968f\u7b14","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.ponybai.com\/?p=2423","og_locale":"zh_CN","og_type":"article","og_title":"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305 - \u767d\u767d\u968f\u7b14","og_description":"\u5b66\u4e60\u76ee\u6807 \u5b8c\u6210\u672c\u5355\u5143\u540e\uff0c\u60a8\u5c06\u80fd\u591f\uff1a \u786e\u5b9a\u53d8\u91cf\u5728JavaScript\u4e2d\u7684\u8303\u56f4\u3002 \u63cf\u8ff0\u5982\u4f55this\u6839\u636e\u8c03\u7528\u51fd\u6570\u7684\u4f4d &hellip; \u7ee7\u7eed\u9605\u8bfbSalesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305","og_url":"http:\/\/www.ponybai.com\/?p=2423","og_site_name":"\u767d\u767d\u968f\u7b14","article_published_time":"2020-06-25T15:28:40+00:00","og_image":[{"url":"https:\/\/res.cloudinary.com\/hy4kyit2a\/image\/upload\/doc\/trailhead\/en-usb473bb5ea1b7e61dfb07e6a7e547de6b.gif"}],"author":"ponybai","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"ponybai","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.ponybai.com\/?p=2423#article","isPartOf":{"@id":"http:\/\/www.ponybai.com\/?p=2423"},"author":{"name":"ponybai","@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883"},"headline":"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305","datePublished":"2020-06-25T15:28:40+00:00","dateModified":"2020-06-25T15:28:40+00:00","mainEntityOfPage":{"@id":"http:\/\/www.ponybai.com\/?p=2423"},"wordCount":39,"publisher":{"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883"},"articleSection":["LWC\u4e2d\u7684Javascript"],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/www.ponybai.com\/?p=2423","url":"http:\/\/www.ponybai.com\/?p=2423","name":"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305 - \u767d\u767d\u968f\u7b14","isPartOf":{"@id":"http:\/\/www.ponybai.com\/#website"},"datePublished":"2020-06-25T15:28:40+00:00","dateModified":"2020-06-25T15:28:40+00:00","breadcrumb":{"@id":"http:\/\/www.ponybai.com\/?p=2423#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.ponybai.com\/?p=2423"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.ponybai.com\/?p=2423#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/www.ponybai.com\/"},{"@type":"ListItem","position":2,"name":"Salesforce\u5f00\u53d1\u4eba\u5458\u7684JavaScript\u6280\u80fd-\u4e86\u89e3\u4e0a\u4e0b\u6587\uff0c\u8303\u56f4\u548c\u95ed\u5305"}]},{"@type":"WebSite","@id":"http:\/\/www.ponybai.com\/#website","url":"http:\/\/www.ponybai.com\/","name":"\u767d\u767d\u968f\u7b14","description":"Salesforce\u5b98\u65b9\u8bb2\u5e08","publisher":{"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.ponybai.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883","name":"ponybai","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/","url":"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg","contentUrl":"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg","width":248,"height":248,"caption":"ponybai"},"logo":{"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/121.37.188.161"],"url":"http:\/\/www.ponybai.com\/?author=1"}]}},"_links":{"self":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts\/2423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2423"}],"version-history":[{"count":0,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts\/2423\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2423"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}