{"id":260460,"date":"2023-01-28T08:09:52","date_gmt":"2023-01-28T00:09:52","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=260460"},"modified":"2022-12-27T23:11:21","modified_gmt":"2022-12-27T15:11:21","slug":"arrify-math-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/arrify-math-linux.html","title":{"rendered":"\u89e3\u6790arrify \u8f6c\u6570\u7ec4\u5b9e\u73b0\u793a\u4f8b\u6e90\u7801"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86arrify \u8f6c\u6570\u7ec4\u5b9e\u73b0\u793a\u4f8b\u6e90\u7801\u89e3\u6790\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u5b66\u4e60\u76ee\u6807<\/strong><\/div>\n
  • \u5206\u6790 arrify \u51fd\u6570\u7684\u6e90\u7801<\/li>\n
  • \u901a\u8fc7\u6d4b\u8bd5\u7528\u4f8b\u8c03\u8bd5\u6e90\u7801<\/li>\n
  • \u5b66\u4e60 Symbol.iterator \u7684\u4f7f\u7528\u573a\u666f<\/li>\n
  • \u5176\u5b83\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/li>\n
    \u62c9\u53d6\u6e90\u7801<\/strong><\/div>\n

    \u8fdb\u5165\u5230 arrify \u4ed3\u5e93\u4e0b\uff0c\u4f7f\u7528 CodeSpace \u514b\u9686\u4e00\u4efd\u9879\u76ee\u3002<\/p>\n

    \u9879\u76ee\u76ee\u5f55\u5982\u56fe\uff1a<\/p>\n

    \"\"<\/p>\n

    \u5ffd\u7565\u6389\u4e00\u4e9b\u914d\u7f6e\u6587\u4ef6\uff0c\u5404\u4e2a\u6587\u4ef6\u7684\u529f\u80fd\u5982\u4e0b\uff1a<\/p>\n

  • index.js\u662f\u6574\u4e2a\u9879\u76ee\u7684\u5165\u53e3\uff0c\u8d1f\u8d23\u5bf9\u5916\u5bfc\u51faarrify\u51fd\u6570<\/li>\n
  • index.d.ts\u662farrify\u51fd\u6570\u7684TS\u7c7b\u578b\u63cf\u8ff0\u6587\u4ef6<\/li>\n
  • test.js\u662f\u6d4b\u8bd5\u7528\u4f8b<\/li>\n
    \u5206\u6790\u6e90\u7801<\/strong><\/div>\n

    arrify\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u503c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8be5\u503c\u7684\u6570\u7ec4,\u6839\u636e\u4f20\u5165\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u8fd4\u56de\u4e0d\u540c\u7684\u7ed3\u679c\u3002<\/p>\n

    export default function arrify(value) {\r\n        \/\/ \u5982\u679c\u4f20\u5165\u7684\u503c\u662f null \u6216 undefined\uff0c\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6570\u7ec4\u3002\r\n    if (value === null || value === undefined) {\r\n        return [];\r\n    }\r\n        \/\/ \u5982\u679c\u4f20\u5165\u7684\u503c\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u51fd\u6570\u4f1a\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e2a\u6570\u7ec4\r\n    if (Array.isArray(value)) {\r\n        return value;\r\n    }\r\n        \/\/ \u5982\u679c\u4f20\u5165\u7684\u503c\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8be5\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002\r\n    if (typeof value === 'string') {\r\n        return [value];\r\n    }\r\n        \/\/ \u5982\u679c\u4f20\u5165\u7684\u503c\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8be5\u5bf9\u8c61\u6240\u6709\u5143\u7d20\u7684\u6570\u7ec4\u3002\r\n    if (typeof value[Symbol.iterator] === 'function') {\r\n        return [...value];\r\n    }\r\n        \/\/ \u5982\u679c\u4f20\u5165\u7684\u503c\u65e2\u4e0d\u662f null\/undefined\uff0c\u4e5f\u4e0d\u662f\u4e00\u4e2a\u6570\u7ec4\/\u5b57\u7b26\u4e32\/\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8be5\u503c\u7684\u6570\u7ec4\u3002\r\n    return [value];\r\n}<\/pre>\n
    Symbol.iterator \u7684\u4f7f\u7528\u573a\u666f<\/strong><\/div>\n

    Symbol.iterator \u662f JavaScript \u4e2d\u7684\u4e00\u4e2a\u5185\u7f6e Symbol\uff0c\u5b83\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u5bf9\u8c61\u7684\u9ed8\u8ba4\u8fed\u4ee3\u5668\u3002\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u7528\u4e8e for...of \u5faa\u73af\u6216\u8005\u89e3\u6784\u8d4b\u503c\u65f6\uff0c\u4f1a\u81ea\u52a8\u8c03\u7528\u5b83\u7684 Symbol.iterator \u65b9\u6cd5\u3002<\/p>\n

    \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u7ec4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 Symbol.iterator \u65b9\u6cd5\u6765\u5b9a\u4e49\u5982\u4f55\u904d\u5386\u8fd9\u4e2a\u6570\u7ec4\uff1a<\/p>\n

    const numbers = [1, 2, 3];\r\nnumbers[Symbol.iterator] = function() {\r\n  let i = 0;\r\n  return {\r\n    next: function() {\r\n      return {\r\n        value: numbers[i] + 1,\r\n        done: i++ === numbers.length\r\n      };\r\n    }\r\n  };\r\n};\r\nfor (const num of numbers) {\r\n  console.log(num);\r\n}<\/pre>\n

    \u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u7ec4 numbers\uff0c\u5e76\u4e3a\u5b83\u5b9a\u4e49\u4e86\u4e00\u4e2a Symbol.iterator \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u7684 next \u65b9\u6cd5\u8fd4\u56de\u6570\u7ec4\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u7136\u540e\u6211\u4eec\u4f7f\u7528 for...of \u5faa\u73af\u6765\u904d\u5386\u8fd9\u4e2a\u6570\u7ec4\uff0c\u5faa\u73af\u4f1a\u81ea\u52a8\u8c03\u7528 numbers \u7684 Symbol.iterator \u65b9\u6cd5\u6765\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20+1\u540e\u7684\u503c\u3002<\/p>\n

    \u4e0a\u9762\u4ee3\u7801\u6267\u884c\u7684\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n

    \"\"<\/p>\n

    \u53ef\u89c1\u6211\u4eec\u66f4\u6539\u4e86 array \u7684\u9ed8\u8ba4\u8fed\u4ee3\u5668\u3002<\/p>\n

    \u5177\u6709\u9ed8\u8ba4\u7684\u8fed\u4ee3\u5668\u51fd\u6570\u7684\u5bf9\u8c61<\/strong><\/div>\n

    \u8fd9\u4e9b\u5bf9\u8c61\u80fd\u591f\u88ab for...of \u5faa\u73af\u904d\u5386<\/p>\n

  • \u6570\u7ec4<\/li>\n
  • \u5b57\u7b26\u4e32<\/li>\n
  • Map\uff08Map\uff09<\/li>\n
  • Set\uff08Set\uff09<\/li>\n

    \u603b\u4e4b\uff0c\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u5982\u679c\u5177\u6709Symbol.iterator\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u53ef\u4ee5\u88abfor...of\u904d\u5386\u5b83\u7684\u6210\u5458\u3002\u6211\u4eec\u7406\u89e3iteration\u7684\u539f\u7406\u53ef\u4ee5\u66f4\u597d\u4f7f\u7528js\u63d0\u4f9b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5fc5\u8981\u65f6\u8fd8\u53ef\u4ee5\u6539\u9020\u4e0d\u53ef\u8fed\u4ee3\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u5206\u6790 arrify \u51fd\u6570\u7684\u6e90\u7801 \u901a\u8fc7\u6d4b\u8bd5\u7528\u4f8b\u8c03\u8bd5\u6e90\u7801 \u5b66\u4e60 Symbol.iterator \u7684\u4f7f\u7528\u573a\u666f \u5176\u5b83\u7684 […]<\/p>\n","protected":false},"author":1479,"featured_media":259033,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-260460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=260460"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260460\/revisions"}],"predecessor-version":[{"id":260467,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260460\/revisions\/260467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/259033"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=260460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=260460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=260460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}