{"id":7826,"date":"2018-07-24T00:29:01","date_gmt":"2018-07-23T16:29:01","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=7826"},"modified":"2018-08-02T01:17:16","modified_gmt":"2018-08-01T17:17:16","slug":"js-padstart-padend","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/07\/js-padstart-padend\/","title":{"rendered":"JS\u5b57\u7b26\u4e32\u8865\u5168\u65b9\u6cd5padStart()\u548cpadEnd()\u7b80\u4ecb"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=7826\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=7826<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\u3002<\/p>\n<h3>\u4e00\u3001\u5173\u4e8e\u5b57\u7b26\u4e32\u8865\u5168<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/7826-thumb.png\" width=\"242\" height=\"150\" alt=\"\u5b57\u7b26\u4e32\u8865\u5168\u6587\u7ae0\u7f29\u7565\u56fe\" class=\"alignright thumb size-medium\" \/><\/p>\n<p>\u5728JS\u4e2d\uff0c\u5b57\u7b26\u4e32\u8865\u5168\u662f\u5e38\u7528\u64cd\u4f5c\uff0c\u7528\u7684\u6bd4\u8f83\u591a\u7684\u5c31\u662f\u65f6\u95f4\u6216\u8005\u65e5\u671f\u524d\u9762\u7684\u8865<code>0<\/code>\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u65e5\u671f\uff0c\u6211\u4eec\u591a\u91c7\u75284-2-2\u7684\u8868\u793a\u5f62\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>2018-07-23<\/pre>\n<p>\u5f53\u6211\u4eec\u4f7f\u7528\u65f6\u95f4\u6233\u8fdb\u884c\u6708\u4efd\u83b7\u53d6\u7684\u65f6\u5019\uff0c\u662f\u6ca1\u6709\u524d\u9762\u7684<code>0<\/code>\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>var month = new Date().getMonth() + 1;    <span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f7<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/2018-07-23_212330.png\" width=\"235\" height=\"79\" alt=\"\u8fd0\u884c\u7ed3\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6b64\u65f6\uff0c\u5c31\u9700\u8981\u8fdb\u884c\u8865\u5168\uff0c\u901a\u5e38\u505a\u6cd5\u662f\u8fd9\u6837\uff1a<\/p>\n<pre>if (month < 10) {\n    month = '0' + month;\n}<\/pre>\n<p>\u751a\u81f3\u4f1a\u4e13\u95e8\u5b9a\u4e49\u4e00\u4e2a\u8865<code>'0'<\/code>\u65b9\u6cd5\uff0c\u4f8b\u5982\u6b64<a href=\"https:\/\/www.zhangxinxu.com\/php\/microCodeDetail?id=10\" rel=\"noopener\" target=\"_blank\">\u65e5\u671f\u8f6c\u65f6\u95f4\u6233\u5fae\u7801<\/a>\u4e2d\u7684\u81ea\u5b9a\u4e49\u7684<code>zero<\/code>\u65b9\u6cd5\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/2018-07-23_213135.png\" width=\"333\" height=\"144\" alt=\"\u8865\u5168\u65b9\u6cd5\u4ee3\u7801\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u7136\u800c\uff0c\u968f\u7740JS\u5b57\u7b26\u4e32\u8865\u5168\u65b9\u6cd5<code>padStart()<\/code>\u548c<code>padEnd()<\/code>\u7684\u51fa\u73b0\uff0c\u7c7b\u4f3c\u573a\u666f\u4f7f\u7528\u5c31\u7b80\u5355\u591a\u4e86\uff01<\/p>\n<h3>\u4e8c\u3001\u5173\u4e8epadStart<\/h3>\n<p><code>padStart<\/code>\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u8fdb\u884c\u5b57\u7b26\u8865\u5168\u3002<\/p>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>str.padStart(targetLength [, padString])<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<dl>\n<dt><strong>targetLength<\/strong> \uff08\u53ef\u9009\uff09<\/dt>\n<dd><code>targetLength<\/code>\u6307\u76ee\u6807\u5b57\u7b26\u4e32\u957f\u5ea6\u3002<\/p>\n<p>\u7136\u540e\uff0c\u6839\u636e\u6211\u7684\u6d4b\u8bd5\uff0c<code>targetLength<\/code>\u53c2\u6570\u7f3a\u7701\u4e5f\u4e0d\u4f1a\u62a5\u9519\uff0c\u539f\u672c\u7684\u5b57\u7b26\u4e32\u539f\u5c01\u4e0d\u52a8\u8fd4\u56de\uff0c\u4e0d\u8fc7\u4ee3\u7801\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u56e0\u6b64\uff0c\u57fa\u672c\u4e0a\u6ca1\u6709\u4f7f\u7528\u7684\u7406\u7531\u3002<\/p>\n<p>\u8fd8\u6709\uff0c<code>targetLength<\/code>\u53c2\u6570\u7684\u7c7b\u578b\u53ef\u4ee5\u662f\u6570\u503c\u7c7b\u578b\u6216\u8005\u5f31\u6570\u503c\u7c7b\u578b\u3002\u5728JavaScript\u4e2d\uff0c<code>1 == '1'<\/code>\uff0c<code>1<\/code>\u662f\u6570\u503c\uff0c<code>'1'<\/code>\u867d\u7136\u672c\u8d28\u4e0a\u662f\u5b57\u7b26\u4e32\uff0c\u4f46\u4e5f\u53ef\u4ee5\u770b\u6210\u662f\u5f31\u6570\u503c\u3002\u5728<code>padStart()<\/code>\u65b9\u6cd5\u4e2d\uff0c\u6570\u503c\u7c7b\u578b\u6216\u8005\u5f31\u6570\u503c\u7c7b\u578b\u90fd\u662f\u53ef\u4ee5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>'zhangxinxu'.padStart('5');<\/pre>\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u5b9e\u9645\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u6ca1\u5fc5\u8981\u5bf9<code>targetLength<\/code>\u53c2\u6570\u8fdb\u884c\u5f3a\u5236\u7684\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>\u6700\u540e\uff0c\u5982\u679c<code>targetLength<\/code>\u8bbe\u7f6e\u7684\u957f\u5ea6\u6bd4\u5b57\u7b26\u4e32\u672c\u8eab\u8fd8\u8981\u5c0f\uff0c\u5219\u539f\u672c\u7684\u5b57\u7b26\u4e32\u539f\u5c01\u4e0d\u52a8\u8fd4\u56de\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>'zhangxinxu'.padStart(5);    \n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u8fd8\u662f'zhangxinxu'<\/span><\/pre>\n<\/dd>\n<dt><strong>padString<\/strong> \uff08\u53ef\u9009\uff09<\/dt>\n<dd><code>padString<\/code>\u8868\u793a\u7528\u6765\u8865\u5168\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u3002\u7136\u800c\uff0c\u867d\u7136\u8bed\u4e49\u4e0a\u662f\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u6839\u636e\u6211\u7684\u6d4b\u8bd5\uff0c\u4efb\u610f\u7c7b\u578b\u7684\u503c\u90fd\u662f\u53ef\u4ee5\u7684\u3002\u65e0\u8bba\u662fChrome\u6d4f\u89c8\u5668\u8fd8\u662fFirefox\u6d4f\u89c8\u5668\uff0c\u90fd\u4f1a\u5c1d\u8bd5\u5c06\u8fd9\u4e2a\u53c2\u6570\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u8fdb\u884c\u8865\u5168\u3002\u4f8b\u5982\u4e0b\u9762\u51e0\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>'zhangxinxu'.padStart(15, false);\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f'falsezhangxinxu'<\/span><\/pre>\n<pre>'zhangxinxu'.padStart(15, null);\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f'nullnzhangxinxu'<\/span><\/pre>\n<pre>'zhangxinxu'.padStart(15, []);\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f'zhangxinxu'\uff0c\u56e0\u4e3a[]\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u662f\u7a7a\u5b57\u7b26\u4e32<\/span><\/pre>\n<pre>'zhangxinxu'.padStart(15, {});\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f'[objezhangxinxu'\uff0c\u53ea\u663e\u793a\u4e86'[object Object]'\u524d5\u4e2a\u5b57\u7b26<\/span><\/pre>\n<p><code>padString<\/code>\u53c2\u6570\u9ed8\u8ba4\u503c\u662f\u666e\u901a\u7a7a\u683c<code>' '<\/code>\uff08U+0020\uff09\uff0c\u4e5f\u5c31\u662f\u6572Space\u7a7a\u683c\u952e\u51fa\u73b0\u7684\u7a7a\u683c\u3002<\/p>\n<p>\u4ece\u4e0a\u9762\u51e0\u4e2a\u6848\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u5982\u679c\u8865\u5168\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u8db3\uff0c\u5219\u4e0d\u65ad\u5faa\u73af\u8865\u5168\uff1b\u5982\u679c\u957f\u5ea6\u8d85\u51fa\uff0c\u5219\u4ece\u5de6\u4fa7\u5f00\u59cb\u4f9d\u6b21\u8865\u5168\uff0c\u6ca1\u6709\u8865\u5230\u7684\u5b57\u7b26\u4e32\u76f4\u63a5\u5c31\u5ffd\u7565\u3002\n<\/dd>\n<\/dl>\n<p>\u6b64\u65b9\u6cd5\u8fd4\u56de\u503c\u662f\u8865\u5168\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u56de\u5230\u4e00\u5f00\u59cb\u7684\u65e5\u671f\u8865<code>'0'<\/code>\u529f\u80fd\uff0c\u6709\u4e86<code>padStart()<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u4ee3\u7801\u53ef\u4ee5\u7b80\u5316\u6210\u4e0b\u9762\u8fd9\u4e00\u884c\uff1a<\/p>\n<pre>var month = String(new Date().getMonth() + 1).padStart(2, '0');    <span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f'07'<\/span><\/pre>\n<p><strong>\u517c\u5bb9\u6027<\/strong><\/p>\n<p>IE14\u4ee5\u5176\u5df2\u4e0b\u6d4f\u89c8\u5668\u90fd\u4e0d\u652f\u6301\uff0c\u8003\u8651\u5230\u73b0\u5728\u8fd8\u662fwindows 7\u5929\u4e0b\uff0cPC\u7aef\u5bf9\u5916\u9879\u76ee\u8fd8\u4e0d\u80fd\u76f4\u63a5\u7528\uff1b\u79fb\u52a8\u7aef\uff0cUC\u6d4f\u89c8\u5668\uff0cQQ\u6d4f\u89c8\u5668\u4e5f\u4e0d\u652f\u6301\u3002\u4f46\u662f\uff0c\u4e5f\u4e0d\u662f\u4e0d\u80fd\u4f7f\u7528\uff0c\u52a0\u4e00\u4e2aPolyfill\u5c31\u597d\u4e86\uff0c\u8fd9\u4e2a\u540e\u9762\u4f1a\u5c55\u793a\u3002<\/p>\n<h3>\u4e09\u3001\u5173\u4e8epadEnd<\/h3>\n<p><code>padEnd<\/code>\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u7684\u540e\u9762\u8fdb\u884c\u5b57\u7b26\u8865\u5168\uff0c\u8bed\u6cd5\u53c2\u6570\u7b49\u90fd\u548c<code>padStart<\/code>\u7c7b\u4f3c\u3002<\/p>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre>str.padEnd(targetLength [, padString])<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<dl>\n<dt><strong>targetLength<\/strong> \uff08\u53ef\u9009\uff09<\/dt>\n<dd><code>targetLength<\/code>\u6307\u8865\u5168\u540e\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<\/p>\n<p>\u7136\u540e\uff0c\u6839\u636e\u6211\u7684\u6d4b\u8bd5\uff0c<code>targetLength<\/code>\u53c2\u6570\u5982\u679c\u4e0d\u8bbe\u7f6e\uff0c\u4e0d\u4f1a\u62a5\u9519\uff0c\u76f4\u63a5\u8fd4\u56de\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u4e0d\u8fc7\u8fd9\u6837\u4ee3\u7801\u5c31\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u56e0\u6b64\uff0c\u5b9e\u9645\u5f00\u53d1\uff0c\u6b64\u53c2\u6570\u80af\u5b9a\u662f\u9700\u8981\u8bbe\u7f6e\u7684\u3002<\/p>\n<p>\u540c\u6837\u7684\uff0c<code>targetLength<\/code>\u53c2\u6570\u7684\u7c7b\u578b\u53ef\u4ee5\u662f\u6570\u503c\u7c7b\u578b\u6216\u8005\u5f31\u6570\u503c\u7c7b\u578b\u3002\u4f8b\u5982\u4e0b\u97622\u4e2a\u7528\u6cd5\u90fd\u662f\u53ef\u4ee5\u7684\uff1a<\/p>\n<pre>'zhangxinxu'.padEnd('15');\n'zhangxinxu'.padEnd(15);<\/pre>\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u5b9e\u9645\u5199\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u6ca1\u5fc5\u8981\u5f3a\u5236<code>targetLength<\/code>\u53c2\u6570\u4e3a\u6570\u503c\u3002<\/p>\n<p>\u6700\u540e\uff0c\u5982\u679c<code>targetLength<\/code>\u8bbe\u7f6e\u7684\u957f\u5ea6\u6bd4\u5b57\u7b26\u4e32\u672c\u8eab\u8fd8\u8981\u5c0f\uff0c\u5219\u539f\u5b57\u7b26\u4e32\u539f\u5c01\u4e0d\u52a8\u8fd4\u56de\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>'zhangxinxu'.padEnd(5);    \n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u8fd8\u662f'zhangxinxu'<\/span><\/pre>\n<p>\u5982\u679c<code>targetLength<\/code>\u662f\u4e00\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u6570\u503c\u7c7b\u578b\uff0c\u4e5f\u662f\u8fd4\u56de\u539f\u59cb\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>'zhangxinxu'.padEnd(false);    \n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u8fd8\u662f'zhangxinxu'<\/span><\/pre>\n<p>\u4f46\u662f\u6709\u610f\u4e49\u5417\uff1f\u6ca1\u610f\u4e49\uff0c\u6240\u4ee5\u4e0d\u8981\u8fd9\u4e48\u7528\u3002\n<\/dd>\n<dt><strong>padString<\/strong> \uff08\u53ef\u9009\uff09<\/dt>\n<dd><code>padString<\/code>\u8868\u793a\u7528\u6765\u8865\u5168\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u3002\u867d\u7136\u8bed\u4e49\u4e0a\u662f\u5b57\u7b26\u4e32\uff0c\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u5404\u79cd\u7c7b\u578b\u3002\u4f8b\u5982\u4e0b\u9762\u51e0\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>'zhangxinxu'.padEnd(15, false);\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f'zhangxinxufalse'<\/span><\/pre>\n<pre>'zhangxinxu'.padEnd(15, null);\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f'zhangxinxunulln'<\/span><\/pre>\n<pre>'zhangxinxu'.padEnd(15, []);\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f'zhangxinxu'\uff0c\u56e0\u4e3a[]\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u662f\u7a7a\u5b57\u7b26\u4e32<\/span><\/pre>\n<pre>'zhangxinxu'.padEnd(15, {});\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f'zhangxinxu[obje'\uff0c\u53ea\u663e\u793a\u4e86'[object Object]'\u524d5\u4e2a\u5b57\u7b26<\/span><\/pre>\n<p>\u4ece\u4e0a\u9762\u51e0\u4e2a\u6848\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u5982\u679c\u8865\u5168\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u8db3\uff0c\u5219\u4ece\u5de6\u5f80\u53f3\u4e0d\u65ad\u5faa\u73af\u8865\u5168\uff1b\u5982\u679c\u957f\u5ea6\u8d85\u51fa\u53ef\u4ee5\u8865\u5168\u7684\u5b57\u7b26\u957f\u5ea6\uff0c\u5219\u4ece\u5de6\u4fa7\u5c3d\u53ef\u80fd\u8865\u5168\uff0c\u8865\u4e0d\u5230\u7684\u6ca1\u529e\u6cd5\uff0c\u53ea\u80fd\u5ffd\u7565\uff0c\u4f8b\u5982<code>'zhangxinxu'.padEnd(15, {})<\/code>\u7b49\u540c\u4e8e\u6267\u884c<code>'zhangxinxu'.padEnd(15, '[object Object]')<\/code>\uff0c\u6700\u591a\u53ea\u80fd\u88655\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64\uff0c\u53ea\u80fd\u8865<code>'[object Object]'<\/code>\u524d5\u4e2a\u5b57\u7b26\uff0c\u4e8e\u662f\u6700\u540e\u7ed3\u679c\u662f\uff1a<code>'zhangxinxu[obje'<\/code>\u3002<\/p>\n<p><code>padString<\/code>\u53c2\u6570\u5982\u679c\u4e0d\u8bbe\u7f6e\uff0c\u5219\u4f1a\u4f7f\u7528\u666e\u901a\u7a7a\u683c<code>' '<\/code>\uff08U+0020\uff09\u4ee3\u66ff\uff0c\u4e5f\u5c31\u662fSpace\u7a7a\u683c\u952e\u6572\u51fa\u6765\u7684\u90a3\u4e2a\u7a7a\u683c\u3002<\/p>\n<p><strong>\u4e3e\u4e00\u4e2a\u5728\u540e\u9762\u8865\u5168\u5b57\u7b26\u4e32\u6848\u4f8b<\/strong><br \/>\n\u5728JS\u524d\u7aef\u6211\u4eec\u5904\u7406\u65f6\u95f4\u6233\u7684\u65f6\u5019\u5355\u4f4d\u90fd\u662f<code>ms<\/code>\u6beb\u79d2\uff0c\u4f46\u662f\uff0c\u540e\u7aef\u540c\u5b66\u8fd4\u56de\u7684\u65f6\u95f4\u6233\u5219\u4e0d\u4e00\u6837\u662f\u6beb\u79d2\uff0c\u53ef\u80fd\u53ea\u670910\u4f4d\uff0c\u4ee5<code>s<\/code>\u79d2\u4e3a\u5355\u4f4d\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u5728\u524d\u7aef\u5904\u7406\u8fd9\u4e2a\u65f6\u95f4\u6233\u7684\u65f6\u5019\uff0c\u4fdd\u9669\u8d77\u89c1\uff0c\u8981\u5148\u505a\u4e00\u4e2a13\u4f4d\u7684\u8865\u5168\uff0c\u4fdd\u8bc1\u5355\u4f4d\u662f\u6beb\u79d2\u3002\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>timestamp = +String(timestamp).padEnd(13, '0');<\/pre>\n<p><strong>\u517c\u5bb9\u6027<\/strong><\/p>\n<p>\u672c\u5b57\u7b26\u4e32API\u5c5e\u4e8eES6\u65b0\u589e\u65b9\u6cd5\uff0cIE14\u4ee5\u5176\u5df2\u4e0b\u6d4f\u89c8\u5668\u90fd\u4e0d\u652f\u6301\uff0c\u90e8\u5206\u56fd\u4ea7\u79fb\u52a8\u7aef\u6d4f\u89c8\u5668\u4e5f\u4e0d\u652f\u6301\u3002\u76ee\u524d\u5bf9\u5916\u9879\u76ee\u4f7f\u7528\u8fd8\u9700\u8981\u9644\u4e0aPolyfill\u4ee3\u7801\u3002<\/p>\n<h3>\u56db\u3001Polyfill\u4ee3\u7801<\/h3>\n<p>\u4ee5\u4e0bPolyfill\u4ee3\u7801\u53d6\u81eapolyfill\u9879\u76ee\u4e2d\u7684string.polyfill.js\uff0c\u5176\u4e2d\u4f7f\u7528\u4f9d\u8d56\u7684<code>repeat()<\/code>\u4e5f\u662fES6\u65b0\u589e\u65b9\u6cd5\uff0c\u56e0\u6b64\uff0c\u5b8c\u6210Polyfill\u4ee3\u7801\u5982\u4e0b\uff0c\u6ce8\u91ca\u90e8\u5206\u6211\u505a\u4e86\u7b80\u5355\u7684\u7ffb\u8bd1\uff0c\u4ee3\u7801\u90e8\u5206\u7b80\u5316\u4e86\u4e9b\u8bb8\u903b\u8f91\uff0c\u540c\u65f6\uff0c<strong>\u4fee\u590d\u4e86\u4e00\u4e2abug<\/strong>\uff0c\u4e0b\u9762\u4ee3\u7801\u7ea2\u8272\u9ad8\u4eae\u90e8\u5206\u5c31\u662f\u4fee\u590d\u5185\u5bb9\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ https:\/\/github.com\/uxitten\/polyfill\/blob\/master\/string.polyfill.js<\/span>\n<span style=\"color:green;\">\/\/ repeat()\u65b9\u6cd5\u7684polyfill<\/span>\nif (!String.prototype.repeat) {\n    String.prototype.repeat = function (count) {\n        'use strict';\n        if (this == null) {\n            throw new TypeError('can\\'t convert ' + this + ' to object');\n        }\n        var str = '' + this;\n        count = +count;\n        if (count != count) {\n            count = 0;\n        }\n        if (count &lt; 0) {\n            throw new RangeError('repeat count must be non-negative');\n        }\n        if (count == Infinity) {\n            throw new RangeError('repeat count must be less than infinity');\n        }\n        count = Math.floor(count);\n        if (str.length == 0 || count == 0) {\n            return '';\n        }\n        if (str.length * count &gt;= 1 &lt;&lt; 28) {\n            throw new RangeError('repeat count must not overflow maximum string size');\n        }\n        var rpt = '';\n        for (; ;) {\n            if ((count &amp; 1) == 1) {\n                rpt += str;\n            }\n            count &gt;&gt;&gt;= 1;\n            if (count == 0) {\n                break;\n            }\n            str += str;\n        }\n        return rpt;\n    }\n}\n<span style=\"color:green;\">\/\/ padStart()\u65b9\u6cd5\u7684polyfill<\/span>\nif (!String.prototype.padStart) {\n    String.prototype.padStart = function (targetLength, padString) {\n        <span style=\"color:green;\">\/\/ \u622a\u65ad\u6570\u5b57\u6216\u5c06\u975e\u6570\u5b57\u8f6c\u6362\u4e3a0<\/span>\n        targetLength = targetLength&gt;&gt;0; \n        padString = String((typeof padString !== 'undefined' ? padString : ' '));\n        if (this.length &gt; targetLength <span style=\"color:#cd0000;\">|| padString === ''<\/span>) {\n            return String(this);\n        }\n        targetLength = targetLength-this.length;\n        if (targetLength &gt; padString.length) {\n            <span style=\"color:green;\">\/\/ \u6dfb\u52a0\u5230\u521d\u59cb\u503c\u4ee5\u786e\u4fdd\u957f\u5ea6\u8db3\u591f<\/span>\n            padString += padString.repeat(targetLength \/ padString.length); \n        }\n        return padString.slice(0, targetLength) + String(this);\n    };\n}\n<span style=\"color:green;\">\/\/ padEnd()\u65b9\u6cd5\u7684polyfill<\/span>\nif (!String.prototype.padEnd) {\n    String.prototype.padEnd = function (targetLength, padString) {\n        <span style=\"color:green;\">\/\/ \u8f6c\u6570\u503c\u6216\u8005\u975e\u6570\u503c\u8f6c\u6362\u62100<\/span>\n        targetLength = targetLength >> 0; \n        padString = String((typeof padString !== 'undefined' ? padString : ' '));\n        if (this.length > targetLength <span style=\"color:#cd0000;\">|| padString === ''<\/span>) {\n            return String(this);\n        }\n        targetLength = targetLength - this.length;\n        if (targetLength > padString.length) {\n            <span style=\"color:green;\">\/\/ \u6dfb\u52a0\u5230\u521d\u59cb\u503c\u4ee5\u786e\u4fdd\u957f\u5ea6\u8db3\u591f<\/span>\n            padString += padString.repeat(targetLength \/ padString.length);\n        }\n        return String(this) + padString.slice(0, targetLength);\n    };\n}<\/pre>\n<p>\u4ee5\u4e0apolyfill\u4ee3\u7801\u9700\u8981\u653e\u5728\u8c03\u7528<code>padStart()<\/code>\/<code>padEnd()<\/code>\u65b9\u6cd5\u7684\u4ee3\u7801\u7684\u524d\u9762\uff0c\u53ea\u8981\u5728\u5408\u9002\u7684\u4f4d\u7f6e\u5c31\u8fd9\u4e48\u4e00\u7c98\u8d34\uff0c\u7136\u540e\uff0c\u65e0\u8bba\u4ec0\u4e48\u7248\u672c\u6d4f\u89c8\u5668\u6d4f\u89c8\u5668\uff0c\u54ea\u6015IE6\uff0cIE8\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528<code>padStart()<\/code>\u6216\u8005<code>padEnd()<\/code>\u65b9\u6cd5\u4e86\u3002<\/p>\n<p><strong>polyfill\u4ee3\u7801\u4e0b\u7684demo\u6848\u4f8b<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/201807\/padstart-padend-polyfill-demo.php\" rel=\"noopener\" target=\"_blank\">padStart\u548cpadEnd\u65b9\u6cd5polyfill\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u539fpolyfill\u65b9\u6cd5\u7684\u4e00\u4e2abug\u5c31\u662f\u901a\u8fc7\u8fd9\u4e2a\u6d4b\u8bd5demo\u6d4b\u51fa\u6765\u7684\uff0c\u4e0b\u9762\u662f\u4fee\u6b63\u540e\u7684polyfill\u4ee3\u7801\u5728IE9\u6d4f\u89c8\u5668\u7684\u6d4b\u8bd5\u7ed3\u679c\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/2018-07-24_001136.png\" width=\"480\" height=\"653\" alt=\"\u6d4b\u8bd5\u7ed3\u679c\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e94\u3001\u7ed3\u8bed<\/h3>\n<p>\u6807\u9898\u867d\u7136\u662f\u7b80\u4ecb\uff0c\u4f46\u662f\u672c\u6587\u5185\u5bb9\u5b9e\u9645\u4e0a\u5df2\u7ecf\u975e\u5e38\u6df1\u5165\u7ec6\u8282\u4e86\u3002<code>padStart()<\/code>\u548c<code>padEnd()<\/code>\u4e24\u4e2a\u65b9\u6cd5\u53c2\u6570\u5bb9\u9519\u6027\u975e\u5e38\u5f3a\uff0c\u975e\u5e38\u6709JS\u7684\u7279\u8272\uff0c\u6211\u5f88\u559c\u6b22\u3002\u4f46\u662f\uff0c\u4e5f\u5e26\u6765\u53e6\u5916\u7684\u95ee\u9898\uff0c\u5c31\u662f\uff0c\u5982\u679c\u6211\u4eec\u7684\u53c2\u6570\u503c\u56e0\u4e3a\u5404\u79cd\u539f\u56e0\uff0c\u6700\u540e\u5e76\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\u53c2\u6570\u503c\uff0c\u5219\u5f88\u53ef\u80fd\u4f1a\u4ea7\u751f\u975e\u5e38\u9690\u853d\u7684bug\uff0c\u56e0\u4e3a\u4e0d\u4f1a\u62a5\u9519\u554a\uff0c\u8fd0\u884c\u8fd8\u662f\u6b63\u5e38\u7684\u3002\u6240\u4ee5\uff0c\u5bb9\u9519\u6027\u5f3a\uff0c\u4e5f\u7b97\u662f\u6709\u5229\u6709\u5f0a\u3002<\/p>\n<p>ES6\u5bf9\u5b57\u7b26\u4e32\u8fd8\u6269\u5c55\u4e86\u5f88\u591a\u5176\u4ed6\u65b9\u6cd5\uff0c\u9664\u4e86\u672c\u6587\u63d0\u5230\u7684<code>repeat()<\/code>\u65b9\u6cd5\uff0c\u8fd8\u6709\u8bf8\u5982<code>normalize()<\/code>\uff0c<code>trimStart()<\/code>\uff0c<code>trimEnd()<\/code>\u7b49API\u65b9\u6cd5\u3002\u8fd9\u4e9b\u4e4b\u540e\u518d\u4e00\u4e2a\u4e00\u4e2a\u6df1\u5165\u7ec6\u8282\u3002<\/p>\n<p>\u8bdd\u8bf4\uff0c\u6211\u73b0\u5728\u624d\u5f00\u59cb\u6df1\u5165\u5b66\u4e60ES6\u7684\u57fa\u7840\u77e5\u8bc6\u5e94\u8be5\u8fd8\u4e0d\u7b97\u665a\u5427\uff0c\u54c8\u54c8\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/emtion\/laugh.gif\" width=\"100\" height=\"100\" alt=\"\u5927\u7b11\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=7826\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=7826<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/7826-thumb.png\" width=\"161\" height=\"100\" alt=\"\u5b57\u7b26\u4e32\u8865\u5168\u6587\u7ae0\u7f29\u7565\u56fe\" class=\"alignright size-medium\" \/> \u5728JS\u4e2d\uff0c\u5b57\u7b26\u4e32\u8865\u5168\u662f\u5e38\u7528\u64cd\u4f5c\uff0c\u4ee5\u524d\u6211\u4eec\u7684\u505a\u6cd5\u662f\u5148\u68c0\u6d4b\u5b57\u7b26\u4e32\u957f\u5ea6\u591f\u4e0d\u591f\uff0c\u4e0d\u591f\uff0c\u81ea\u5df1\u518d\u62fc\u63a5\u5b57\u7b26\u4e32\u4ee5\u5230\u8fbe\u6211\u4eec\u9700\u8981\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u3002\u5230\u4e86ES6\uff0c\u6d4f\u89c8\u5668\u5929\u7136\u652f\u6301\u5b57\u7b26\u4e32\u957f\u5ea6\u8865\u5168\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u662f<code>padStart()<\/code>\u548c<code>padEnd()<\/code>\u3002\u4e4d\u4e00\u770b\u597d\u50cf\u5f88\u7b80\u5355\uff0c\u4f46\u662f\uff0c\u4ed4\u7ec6\u4e00\u6df1\u5165\uff0c\u53d1\u73b0\u610f\u60f3\u4e0d\u5230\u7684\u7ec6\u8282\u8fd8\u633a\u591a\u7684\uff0c\u8981\u4e0d\uff0c\u4f60\u8fdb\u6765\u7785\u4e00\u7785\uff1f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[904,1281,1282,1052,1283,371],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7826"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=7826"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7826\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=7826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=7826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=7826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}