{"id":10233,"date":"2020-04-29T17:36:18","date_gmt":"2020-04-29T08:36:18","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=10233"},"modified":"2023-05-27T20:57:26","modified_gmt":"2023-05-27T11:57:26","slug":"%e3%80%90javascript%e3%80%91%e3%80%8c%e3%82%a4%e3%83%86%e3%83%ac%e3%83%bc%e3%82%bf%e3%83%bc%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=10233","title":{"rendered":"\u3010JavaScript\u3011\u300c\u30b7\u30f3\u30dc\u30eb\u300d\u3001\u300c\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u300d\u3001\u300c\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>Symbol\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/h2>\n<p>\u65b0\u3057\u304f\u8ffd\u52a0\u3055\u308c\u305f<span style=\"color: #ff0000;\"><strong>Symbol<\/strong><\/span>\u3068\u3044\u3046\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002<\/p>\n<h3>\u7528\u9014<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u901a\u5e38\u306e\u958b\u767a\u3067\u306f\u3042\u307e\u308a\u4f7f\u3046\u6a5f\u4f1a\u306f\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/strong><\/span>JavaScript\u306f\u6b74\u53f2\u306e\u3042\u308b\u8a00\u8a9e\u306a\u306e\u3067\u81a8\u5927\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u3042\u308a\u5909\u6570\u304c\u7af6\u5408\u3057\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308b\u306e\u3067\u30b7\u30f3\u30dc\u30eb\u3092\u4f7f\u3063\u3066\u7af6\u5408\u3092\u6e1b\u3089\u3059\u3068\u3044\u3046\u76ee\u7684\u3067\u4f7f\u308f\u308c\u3066\u3044\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<h3>\u57fa\u672c<\/h3>\n<pre class=\"lang:default decode:true\">const symbol = Symbol();<\/pre>\n<p>\u30b7\u30f3\u30dc\u30eb\u306f\u4e00\u5ea6\u4f5c\u3063\u305f\u5834\u5408\u306f\u5fc5\u305a<span style=\"color: #ff0000;\"><strong>\u5225\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u306a\u308a\u4e00\u81f4\u306f\u3057\u307e\u305b\u3093\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">const symbol = Symbol();\r\nconst symbol2 = Symbol();\r\nconsole.log(symbol === symbol2); \/\/false<\/pre>\n<h4>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u306b\u3082\u3067\u304d\u308b\u3002<\/h4>\n<p>\u30b7\u30f3\u30dc\u30eb\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u306b\u3082\u3067\u304d\u307e\u3059\u304c\u3001<span style=\"color: #ff0000;\"><strong>\u901a\u5e38\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u3068\u7570\u306a\u308a\u30c9\u30c3\u30c8(.)\u3092\u4f7f\u3063\u3066\u30a2\u30af\u30bb\u30b9\u3059\u308b\u308f\u3051\u3067\u306f\u306a\u3044<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let obj = {\r\n  name: \"\u592a\u90ce\",\r\n  age: 25,\r\n  [symbol]: 2\r\n}\r\nconsole.log(obj.name);      \/\/\u592a\u90ce\r\nconsole.log(obj[symbol]); \/\/2<\/pre>\n<h3>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4e2d\u306e\u30b7\u30f3\u30dc\u30eb\u3092\u53d6\u5f97\u3059\u308b\u3002<\/h3>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5185\u306e\u30b7\u30f3\u30dc\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u306b\u306f<span style=\"color: #ff0000;\"><strong>\u300cgetOwnPropertySymbols\u300d<\/strong><\/span>\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">console.log(Object.getOwnPropertyNames(obj));    \/\/\u3053\u308c\u3067\u306f\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044\u3002\r\nconsole.log(Object.getOwnPropertySymbols(obj)); \/\/\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3002<\/pre>\n<p>\u623b\u308a\u5024\u306f\u914d\u5217\u306a\u306e\u3067\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3059\u308c\u3070<span style=\"color: #ff0000;\"><strong>\u30b7\u30f3\u30dc\u30eb\u305d\u306e\u3082\u306e\u306b\u30a2\u30af\u30bb\u30b9\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">console.log(Object.getOwnPropertySymbols(obj)[0]);<\/pre>\n<h2>\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3068\u306f\uff1f<\/h2>\n<p><span style=\"color: #ff0000;\"><strong>\u9806\u756a\u306b\u51e6\u7406\u3067\u304d\u308b\u30c7\u30fc\u30bf\u306e\u3053\u3068<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<h3>\u69cb\u6587<\/h3>\n<p>Symbol\u306b\u5bfe\u3057\u3066iterator\u3068\u3044\u3046\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u4e8b\u3067\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3092\u53d6\u5f97\u3059\u308b\u4e8b\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let a = \"a\";\r\nlet iterator = a[Symbol.iterator]();\r\niterator;<\/pre>\n<h4>\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3068\u3057\u3066\u5b9a\u7fa9\u3067\u304d\u308b\u30c7\u30fc\u30bf\u578b<\/h4>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3068\u3057\u3066\u5b9a\u7fa9\u3067\u304d\u308b\u30c7\u30fc\u30bf\u578b\u306f\u6c7a\u307e\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>string<\/li>\n<li>\u914d\u5217<\/li>\n<\/ul>\n<h4>next\u95a2\u6570<\/h4>\n<p>next\u95a2\u6570\u3092\u4f7f\u3046\u3068<span style=\"color: #ff0000;\"><strong>\u30a4\u30c6\u30ec\u30fc\u30bf\u304b\u3089\u4e00\u3064\u305a\u3064\u5024\u3092\u53d6\u308a\u51fa\u3059\u4e8b\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002\u4e00\u5ea6\u53d6\u308a\u51fa\u3057\u305f\u5834\u6240\u306f\u8a18\u9332\u3055\u308c\u308b\u306e\u3067\u53d6\u308a\u51fa\u3059\u5ea6\u306b\u5024\u304c\u5909\u308f\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">let a = \"abc\";\r\nlet iterator = a[Symbol.iterator]();\r\nlet b = iterator.next();\r\nb; \/\/{ value: 'a', done: false}\r\nlet c = iterator.next();\r\nc; \/\/{ value: 'b', done: false}\r\nlet d = iterator.next();\r\nd; \/\/{ value: 'c', done: false}\r\nlet e = iterator.next();\r\ne; \/\/{ value: undefined, done: true}<\/pre>\n<h5>value<\/h5>\n<p>\u30a4\u30c6\u30ec\u30fc\u30bf\u3067\u53d6\u308a\u51fa\u305b\u305f\u5024\u3067\u3059\u3002\u3082\u3046\u5024\u3092\u53d6\u308a\u51fa\u305b\u306a\u3044\u5834\u5408\u306f<span style=\"color: #ff0000;\"><strong>undefined<\/strong><\/span>\u304c\u8fd4\u308a\u307e\u3059\u3002<\/p>\n<h5>done<\/h5>\n<p>\u53d6\u308a\u51fa\u3057\u304c\u5b8c\u4e86\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u306ffalse\u3001\u53d6\u308a\u51fa\u3057\u304c\u5b8c\u4e86\u3057\u305f\u5834\u5408\u306ftrue\u304c\u8fd4\u308a\u307e\u3059\u3002<\/p>\n<h3>\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u306e\u81ea\u4f5c<\/h3>\n<p>\u30c7\u30fc\u30bf\u578b\u304cstring\u3084\u914d\u5217\u306e\u5834\u5408\u306f\u81ea\u52d5\u3067\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3068\u3057\u3066\u5024\u3092\u53d6\u308a\u51fa\u3059\u4e8b\u304c\u53ef\u80fd\u3067\u3059\u304c\u3001\u81ea\u5206\u3067\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u81ea\u4f5c\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<span style=\"color: #ff0000;\"><strong>next\u95a2\u6570\u3092\u7528\u610f\u3057\u3066\u305d\u306e\u4e2d\u3067value\u3068done\u3092\u8fd4\u3059\u3088\u3046\u306a\u95a2\u6570\u3092\u4f5c\u308c\u3070\u81ea\u4f5c\u3059\u308b\u4e8b\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let a = {\r\n  [Symbol.iterator]() {\r\n    let data = [1, 2, 3]\r\n    let num = 0;\r\n    return {\r\n      next() {\r\n        return {\r\n          value: data[num],\r\n          done: num++ &gt; 2\r\n        }\r\n      }\r\n    }\r\n  }\r\n}\r\nlet iterator = a[Symbol.iterator]();\r\nlet b = iterator.next();\r\nb; \/\/{ value: 1,done: false }\r\nlet c = iterator.next();\r\nc; \/\/{ value: 2,done: false }\r\nlet d = iterator.next();\r\nd; \/\/{ value: 3,done: false }\r\nlet e = iterator.next();\r\ne; \/\/{ value: undefined,done: true }<\/pre>\n<h2>\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3068\u306f\uff1f<\/h2>\n<p>ECMAScript2015\u304b\u3089\u8ffd\u52a0\u3055\u308c\u305f\u95a2\u6570\u306b\u4f3c\u305f\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<h3>\u7528\u9014<\/h3>\n<p>\u623b\u308a\u5024\u3068\u3057\u3066iterator\uff08\u5024\u3092\u4fdd\u6301\u3057\u3066\u304f\u308c\u308b\uff09\u3092\u8fd4\u3059\u306e\u3067\u3001<span style=\"color: #ff0000;\"><strong>\u547c\u3073\u51fa\u3059\u5ea6\u306b\u5024\u3092\u66f4\u65b0\u3057\u3066\u305d\u306e\u5024\u3092\u4fdd\u6301\u3059\u308b\u51e6\u7406<\/strong><\/span>\u3092\u4f5c\u308b\u969b\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h3>\u57fa\u672c\u69cb\u6587<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">function* gene() {\r\n  yield 1;\r\n  yield 2;\r\n}\r\nlet a = gene();\r\na;\r\nconsole.log(a.next()); \/\/ { value:1, done: false }\r\nconsole.log(a.next()); \/\/ { value:2, done: false }\r\nconsole.log(a.next()); \/\/ { value:undefined, done: true }<\/pre>\n<h4>\u30dd\u30a4\u30f3\u30c8<\/h4>\n<ul>\n<li>function\u306e\u672b\u5c3e\u306b*(\u30a2\u30b9\u30bf\u30ea\u30b9\u30af)\u3092\u4ed8\u3051\u308b\u3002<\/li>\n<li>function\u5185\u306byield\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u4f7f\u3046\u3002<\/li>\n<li>\u623b\u308a\u5024\u3068\u3057\u3066Iterator\u3092\u8fd4\u3059\u3002value\u306fyield\u3068\u3057\u3066\u5b9a\u7fa9\u3057\u305f\u5024\u304c\u8fd4\u308b\u3002<\/li>\n<\/ul>\n<h4>\u30a4\u30c6\u30ec\u30fc\u30bf\u306enext\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3057\u305f\u969b\u306e\u6319\u52d5<\/h4>\n<p><span style=\"color: #ff0000;\"><strong>\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u306enext\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3057\u305f\u969b\u306fyield\u304c\u914d\u7f6e\u3057\u3066\u3042\u308b\u7b87\u6240\u307e\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/strong><\/span>\u306a\u306e\u3067\u4e0b\u8a18\u306e\u4f8b\u3067\u8a00\u3048\u3070\u300c\u3042\u300d\u306e\u307f\u304c\u30b3\u30f3\u30bd\u30fc\u30eb\u51fa\u529b\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u3064\u307e\u308ayield\u306f\u901a\u5e38\u306e\u95a2\u6570\u3067\u3044\u3046\u3068\u3053\u308d\u306ereturn\u3068\u4f3c\u305f\u6319\u52d5\u306b\u306a\u308a\u307e\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">function* gene() {\r\n  console.log('\u3042');\r\n  yield 1;\r\n  console.log('\u3044');\r\n  yield 2;\r\n}\r\nlet a = gene();\r\na;\r\nconsole.log(a.next());\u3000\/\/\u300c\u3042\u300d\u3068\u300c{value:1,done:false}\u300d\u304c\u51fa\u529b\u3055\u308c\u308b\u3002<\/pre>\n<h3>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5185\u3067\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u5b9a\u7fa9\u3059\u308b\u306b\u306f\uff1f<\/h3>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5185\u3067\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u5b9a\u7fa9\u3059\u308b\u5834\u5408\u306f<span style=\"color: #ff0000;\"><strong>function\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u6392\u9664\u3057\u3066\u5b9a\u7fa9<\/strong><\/span>\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let person = {\r\n  name: \"\u592a\u90ce\",\r\n  age: 25,\r\n  * gene() {\r\n    yield 1;\r\n  }\r\n}\r\nconsole.log(person.gene().next()); \/\/{ value:1, done: false }<\/pre>\n<h3>\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>yield*\u306e\u5f8c\u306b\u914d\u5217\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3067\u8907\u6570\u884c\u306byield\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u4e26\u3079\u308b\u8a18\u8ff0\u3068\u540c\u7b49\u306e\u8a18\u8ff0\u3092\u3059\u308b\u4e8b\u304c\u3067\u304d\u307e\u3059\u3002<\/strong><\/span>yield\u306e\u9593\u306b\u51e6\u7406\u306f\u631f\u3081\u307e\u305b\u3093\u304c\u3001\u5358\u7d14\u306byield\u3092\u4e26\u3079\u308b\u3060\u3051\u306e\u51e6\u7406\u3067\u3042\u308c\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u65b9\u304c\u7f8e\u3057\u3044\u30b3\u30fc\u30c9\u3068\u8a00\u3048\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true  \">function* gene() {\r\n  yield* [1, 2];\r\n}\r\nlet a = gene();\r\nconsole.log(a.next()); \/\/ {value:1,done:false}\r\nconsole.log(a.next()); \/\/ {value:2,done:false}<\/pre>\n","protected":false},"excerpt":{"rendered":"Symbol\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 \u65b0\u3057\u304f\u8ffd\u52a0\u3055\u308c\u305fSymbol\u3068\u3044\u3046\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002 \u7528\u9014 \u901a\u5e38\u306e\u958b\u767a\u3067\u306f\u3042\u307e\u308a\u4f7f\u3046\u6a5f\u4f1a\u306f\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002JavaScript\u306f\u6b74\u53f2\u306e\u3042\u308b\u8a00\u8a9e\u306a\u306e\u3067\u81a8\u5927\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u3042\u308a\u5909\u6570\u304c\u7af6\u5408\u3057\u3066\u3057\u307e\u3046 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/10233"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10233"}],"version-history":[{"count":9,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/10233\/revisions"}],"predecessor-version":[{"id":20287,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/10233\/revisions\/20287"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10233"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}