{"id":124,"date":"2018-03-18T21:16:54","date_gmt":"2018-03-18T13:16:54","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=124"},"modified":"2020-06-20T23:05:27","modified_gmt":"2020-06-20T15:05:27","slug":"ecmascript%e5%ae%9e%e7%8e%b0%e4%b8%ad%e7%9a%84%e8%8c%83%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2018\/03\/18\/ecmascript%e5%ae%9e%e7%8e%b0%e4%b8%ad%e7%9a%84%e8%8c%83%e5%9e%8b\/","title":{"rendered":"Ecmascript\u5b9e\u73b0\u4e2d\u7684\u8303\u578b"},"content":{"rendered":"<hr \/>\n<p>\u7531\u4e8e\u524d\u7aef\u5f00\u53d1\u53c8\u5927\u91cf\u7528\u5230\u4e86\u8fd9\u4e9b\u6280\u672f\uff0c\u8fd9\u91cc\u628a\u51e0\u5e74\u524d\u57fa\u4e8eECMAScript 262-3\u6807\u51c6\u7684\u8303\u578b\u89e3\u91ca\u7528\u6700\u65b0\u7684ECMASript 2018+\u6807\u51c6\u518d\u6b21\u8be0\u91ca\u3002<\/p>\n<p>\u540c\u5176\u4ed6\u5f3a\u7c7b\u578b\u3001\u9884\u7f16\u8bd1\u4e3a\u4e8c\u8fdb\u5236\u6587\u4ef6\u3001\u9762\u5411\u5bf9\u8c61\u7684ECMAScript\u4e00\u6837\uff0c\u8303\u578b\u4e5f\u662f\u8f83\u5e38\u7528\u7684\u4e00\u79cd\u7f16\u7a0b\u65b9\u5f0f\u3002<\/p>\n<h2>\u8bed\u6cd5<\/h2>\n<p>\u9996\u5148\u770b\u65b0\u7248ECMAScript 2018+\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">function indeex&lt;T&gt;(arg: T): T {\n    console.log(arg);\n    return arg;\n}\nlet result = indeex&lt;string&gt;('hello generics');\n<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c<code>indeex<\/code>\u51fd\u6570\u6ca1\u6709\u505a\u4efb\u4f55\u6709\u7528\u7684\u4e8b\u60c5\uff0c\u53ea\u662f\u544a\u8bc9\u6211\u4eec\u6cdb\u578b\u51fd\u6570\u600e\u4e48\u5199\uff0c\u5c31\u662f\u5c16\u62ec\u53f7\u5185\u5927\u5199\u7684T\uff08\u5176\u4ed6\u4e5f\u884c\uff0c\u522b\u8f83\u771f&#8230;\uff09\u3002\u5f53\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff1a\u4f20\u5165\u4e00\u4e2a\u548cT\u7c7b\u578b\u76f8\u540c\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u76f8\u540c\u7c7b\u578bT\u7684\u503c\u3002\u5176\u4e2dT\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\uff0c<code>any<\/code>, <code>string<\/code>, <code>number<\/code>, <code>class<\/code>, <code>object<\/code>, <code>*int64<\/code>, <code>*float32<\/code> \u7b49\u3002<\/p>\n<p>\u8981\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u7b2c\u4e00\u4e2a\u662f\u5728\u6211\u4eec\u660e\u786e\u8bf4\u51fa\u7c7b\u578b\u7684\u4f8b\u5b50\u4e2d\u6f14\u793a\u7684\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u6211\u4eec\u4f20\u9012\u8fd9\u4e2a\u503c\u3002<\/p>\n<p>\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528ECMAScript\u7684\u7c7b\u578b\u68c0\u6d4b:<\/p>\n<pre><code class=\"language-javascript line-numbers\">function indeex&lt;T&gt;(arg: T): T {\n    console.log(arg);\n    return arg;\n}\nlet result = indeex('hello generics');\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u6ca1\u6709\u6307\u5b9a\u7c7b\u578b\uff0c\u4f46\u7531\u4e8e\u5f53\u8c03\u7528\u51fd\u6570\u4f20\u5165\u53c2\u6570\u65f6\u8fd4\u56de\u7684\u662f\u76f8\u540c\u7c7b\u578b\u7684\u503c\uff0cECMAScript\u7684\u7f16\u8bd1\u5668\u76f4\u63a5\u8fd4\u56de\u4e0e\u4f20\u5165\u7c7b\u578b\u76f8\u540c\u7684\u7c7b\u578b\u5c31\u53ef\u4ee5\u4e86, \u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a<code>\u9690\u5f0f\u8f6c\u6362<\/code>\uff0c\u8fd9\u662f\u7f16\u8bd1\u5668\u81ea\u5e26\u7684\u80fd\u529b:<\/p>\n<p><img src=\"http:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/power_of_generics1.png\" alt=\"Ecmascript\u5b9e\u73b0\u4e2d\u7684\u8303\u578b\" \/><\/p>\n<h2>AJAX\u8bf7\u6c42<\/h2>\n<p>\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u53d1\u51fa\u4e00\u4e2aAjax\u8bf7\u6c42\uff0c\u8fd4\u56de\u4e00\u4e2aJSON\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u89e3\u6790\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u6216\u7c7b\u3002\u8be5\u529f\u80fd\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"line-numbers\">function getAsync(url: string) {\n    return fetch(url)\n        .then((response: Response) =&gt; response.json());\n}\n<\/code><\/pre>\n<p>\u7528<code>fetch API<\/code>\u8bf7\u6c42\u6570\u636e\uff0c\u7528<code>Promise<\/code>\u8fdb\u884c\u5f02\u6b65\u8c03\u7528\uff0c\u8c03\u7528\u5b8c\u6210\u540e\uff0c\u5728\u5f97\u5230\u670d\u52a1\u5668\u54cd\u5e94\u540e\u8c03\u7528json\u51fd\u6570\uff0c\u8fd4\u56de\u7684\u6570\u636e\u53ef\u4ee5\u662f\u4efb\u4f55\u4f60\u60f3\u8981\u7684\uff1a<\/p>\n<p><img src=\"http:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/power_of_generics2.png\" alt=\"Ecmascript\u5b9e\u73b0\u4e2d\u7684\u8303\u578b\" \/><\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u901a\u7528\uff0c\u53ef\u4ee5\u4e0d\u65ad\u7684\u590d\u7528\u3002\u5f53\u7136\u4e5f\u53ef\u4ee5\u5199\u4e00\u4e2a\u5904\u7406\u9519\u8bef\u7684catch\u65b9\u6cd5\u3002<\/p>\n<p>\u8fd9\u6837\u5199\u7684\u597d\u5904\u4e4b\u4e00\u662f\u7c7b\u578b\u68c0\u67e5\uff0c\u81ea\u52a8\u68c0\u6d4b\u5df2\u77e5\u7c7b\u578b\uff0c\u6bd4\u5982\u83b7\u53d6\u7535\u5f71\u7684\u4fe1\u606f\u65f6\uff1a<\/p>\n<pre><code class=\"line-numbers\">class Movie { title: string; }\n\nfunction getAsync&lt;T&gt;(url: string): Promise&lt;T[]&gt; {\n    return fetch(url)\n        .then((response: Response) =&gt; response.json());\n}\n\ngetAsync&lt;Movie&gt;(\"\/movies\")\n  .then(movies =&gt; {\n    movies.forEach(movie =&gt; {\n      console.log(movie.title);\n    });\n  });\n<\/code><\/pre>\n<p><code>getASync<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a<code>Promise<\/code>\uff0c\u5f53<code>Promise<\/code>\u88ab\u89e3\u6790\u65f6\u5f97\u5230\u4e00\u4e2aT\u7c7b\u578b\u7684\u6570\u7ec4\u3002\u7f16\u8bd1\u5668\u4fbf\u80fd\u81ea\u52a8\u8bc6\u522b\u8c03\u7528<code>getAsync<\/code>\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u53d8\u91cf\u7c7b\u578b\u3002<\/p>\n<p><img src=\"http:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/power_of_generics3.gif\" alt=\"Ecmascript\u5b9e\u73b0\u4e2d\u7684\u8303\u578b\" \/><\/p>\n<p>\u5373\u4f7f\u6211\u6ca1\u6709\u6307\u5b9a\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\u3002<\/p>\n<p><img src=\"http:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/power_of_generics4.gif\" alt=\"Ecmascript\u5b9e\u73b0\u4e2d\u7684\u8303\u578b\" \/><\/p>\n<p>\u77e5\u9053\u4e86\u8fd9\u4e9b\u5c31\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528\u8303\u578b\u4e86\u3002<\/p>\n<h3>\u4f7f\u7528<\/h3>\n<p>\u5148\u770b\u8303\u578b\u7684\u8fd0\u884c\u8fc7\u7a0b\u3002<\/p>\n<p>\u5047\u8bbe\u5728\u83b7\u53d6\u8bf7\u6c42\u65f6\u7684\u670d\u52a1\u5668\u5730\u5740\u662f<code>\/api\/v1\/<\/code>\uff0c\u7136\u540e\u518d\u8c03\u7528\u67d0\u4e2a\u5c5e\u6027\u7684\u65f6\u5019\uff0c\u4e0d\u662f\u5c06URL\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u800c\u662f\u5305\u542b\u5c5e\u6027\u7684\u5bf9\u8c61\uff08\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u7684\u65b9\u6cd5\uff09\u3002<\/p>\n<p>\u800c\u73b0\u5728\u9700\u8981\u7684\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code class=\"line-numbers\">function getAsync&lt;T&gt;(arg: T): Promise&lt;T[]&gt; {\n    return fetch(`\/api\/v1\/${arg.resourceName}`)\n        .then((response: Response) =&gt; response.json());\n}\n<\/code><\/pre>\n<p>\u4f46\u662f\u8fd9\u4e48\u5199\uff0c\u7f16\u8bd1\u5668\u4f1a\u63d0\u793a\u5728\u7c7b\u578bT\u4e2d\u4e0d\u5b58\u5728<code>resourceName<\/code>\u3002<\/p>\n<p><img src=\"http:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/power_of_generics5.png\" alt=\"Ecmascript\u5b9e\u73b0\u4e2d\u7684\u8303\u578b\" \/><\/p>\n<p>\u6b64\u65f6\uff0c\u51fd\u6570\u65e0\u6cd5\u77e5\u9053T\u4f1a\u662f\u54ea\u79cd\u7c7b\u578b\u548c\u5c5e\u6027\u3002<\/p>\n<p>\u6240\u4ee5\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u63a5\u53e3<code>Resource<\/code>\uff0c\u5e76\u8ba9\u7c7b\u578bT\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\uff1a<\/p>\n<pre><code class=\"line-numbers\">interface Resource { resourceName: string; }\n\nfunction getAsync&lt;T extends Resource&gt;(arg: T): Promise&lt;T[]&gt; {\n    return fetch(`\/api\/v1\/${arg.resourceName}`)\n        .then((response: Response) =&gt; response.json());\n}\n<\/code><\/pre>\n<p>\u63a5\u7740\uff0c\u66f4\u6539<code>Movie<\/code>\u7c7b\u6765\u5b9e\u73b0\u63a5\u53e3<code>Resource<\/code>\uff0c\u518d\u6765\u8c03\u7528<code>getAsync<\/code>\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"line-numbers\">class Movie implements Resource {\n  title: string;\n  resourceName: string = \"movies\";\n}\ngetAsync&lt;Movie&gt;(new Movie())\n  .then(movies =&gt; {\n    movies.forEach(movie =&gt; {\n      console.log(movie.title);\n    });\n  });\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u68c0\u6d4b\uff0c\u4e0d\u6307\u5b9a\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"line-numbers\">getAsync(new Movie())\n  .then(movies =&gt; {\n    movies.forEach(movie =&gt; {\n      console.log(movie.title);\n    });\n  });\n<\/code><\/pre>\n<h2>\u7528\u4f8b<\/h2>\n<p>\u5047\u8bbe\u8981\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8c03\u7528Ajax\u8bf7\u6c42\uff0c\u6839\u636e<code>id<\/code>\u5c5e\u6027\u8fd4\u56de\u9700\u8981\u7684\u8d44\u6e90\uff1a<\/p>\n<pre><code class=\"line-numbers\">function getSingleAsync&lt;T extends Resource&gt;(arg: T): Promise&lt;T&gt; {\n  return fetch(`\/api\/v1\/${arg.resourceName}\/${arg.id}`)\n    .then((response: Response) =&gt; response.json());\n}\n<\/code><\/pre>\n<p>\u540c\u6837\u7684\u95ee\u9898\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u77e5\u9053\u7c7b\u578bT\u662f\u5426\u5177\u6709id\u5c5e\u6027\u3002\u9700\u8981\u521b\u5efa\u53e6\u4e00\u4e2a\u63a5\u53e3\u544a\u8bc9\u6cdb\u578b\u51fd\u6570T\u7c7b\u5b9e\u73b0\u4e24\u4e2a\u63a5\u53e3\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<pre><code class=\"line-numbers\">class Movie implements Resource, Identifiable {\n  title: string;\n  resourceName: string = \"movies\";\n  id: number;\n}\n\nfunction getSingleAsync&lt;T extends Resource &amp; Identifiable&gt;(arg: T): Promise&lt;T&gt; {\n  return fetch(`\/api\/v1\/${arg.resourceName}\/${arg.id}`)\n    .then((response: Response) =&gt; response.json());\n}\n\nlet movieToFind = new Movie();\nmovieToFind.id = 1;\ngetSingleAsync(movieToFind)\n  .then(movie =&gt; {\n    console.log(movie.title);\n  });\n<\/code><\/pre>\n<p>\u8fd9\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u518d\u62a5\u9519\uff0c\u7c7b\u578b\u4e5f\u80fd\u786e\u5b9a\u3002<\/p>\n<p><img src=\"http:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/power_of_generics6.gif\" alt=\"Ecmascript\u5b9e\u73b0\u4e2d\u7684\u8303\u578b\" \/><\/p>\n<p>code enjoy! \u0295\u2022\u1d25\u2022\u0294<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/indeex.cc\/\">http:\/\/indeex.cc<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u4e8e\u524d\u7aef\u5f00\u53d1\u53c8\u5927\u91cf\u7528\u5230\u4e86\u8fd9\u4e9b\u6280\u672f\uff0c\u8fd9\u91cc\u628a\u51e0\u5e74\u524d\u57fa\u4e8eECMAScript 262-3\u6807\u51c6\u7684\u8303\u578b\u89e3\u91ca\u7528\u6700<a href=\"https:\/\/blog.indeex.club\/index.php\/2018\/03\/18\/ecmascript%e5%ae%9e%e7%8e%b0%e4%b8%ad%e7%9a%84%e8%8c%83%e5%9e%8b\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/124"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":131,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions\/131"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}