{"id":965,"date":"2022-04-03T21:27:11","date_gmt":"2022-04-03T13:27:11","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=965"},"modified":"2023-02-16T14:41:01","modified_gmt":"2023-02-16T06:41:01","slug":"16-javascript-zhong-de-new-guan-jian-zi","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=965","title":{"rendered":"16 &#8211; JavaScript \u4e2d\u7684 new \u5173\u952e\u5b57"},"content":{"rendered":"<p>\u539f\u6587\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/the-new-keyword-in-javascript-45jb\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/the-new-keyword-in-javascript-45jb<\/a><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16489965164203.jpg\" alt=\"\" \/><\/p>\n<p>\u6211\u4eec\u4e4b\u524d\u5df2\u7ecf\u8bb2\u8fc7\u4e86 JavaScript \u4e2d\u4e00\u5207\u7686\u5bf9\u8c61\uff0c\u4f46\u5bf9\u4e8e\u4e86\u89e3\u5982\u4f55\u521b\u5efa\u65b0\u5bf9\u8c61\u6216\u8005\u5bf9\u8c61\u7684\u5b9e\u4f8b\u4e5f\u662f\u9700\u8981\u7684\u3002<\/p>\n<p>\u5728 JavaScipt \u4e2d\u4f60\u53ef\u80fd\u4f1a\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u521b\u5efa\u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const myObject = {\n    name: &quot;Parwinder&quot;,\n    age: 33\n};\n\nconsole.log(myObject); \/\/ { name: 'Parwinder', age: 33 }\n<\/code><\/pre>\n<p>\u4e0a\u9762\u5c31\u662f\u5bf9\u8c61\u5b57\u9762\u91cf\u5f62\u5f0f\u3002<\/p>\n<pre><code class=\"language-plain_text\">const myDate = new Date(&quot;06\/01\/2020&quot;);\nconsole.log(myDate); \/\/ Mon Jun 01 2020 00:00:00 GMT-0500 (Central Daylight Time)\n<\/code><\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u5219\u662f\u4f7f\u7528 <strong>new<\/strong> \u5173\u952e\u5b57\uff0c\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u662f\u521b\u5efa\u4e86\u5bf9\u8c61\u7684\u65b0\u5b9e\u4f8b\u3002<strong>new<\/strong> \u5173\u952e\u5b57\u53ef\u4ee5\u4f5c\u7528\u4e8e\u4efb\u4f55\u6709 <strong>constructor<\/strong> \u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u76f8\u540c\u7684\u5bf9\u8c61\u521d\u59cb\u5316\u6216\u5bf9\u8c61\u5b57\u9762\u91cf\u5f62\u5f0f\u4e5f\u4f1a\u521b\u5efa\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u5c31\u50cf\u662f\u901a\u8fc7 <strong>new Object()<\/strong> \u521b\u5efa\u7684\u4e00\u6837\u3002<\/p>\n<h3><a id=\"%E5%88%9B%E5%BB%BA%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%AF%B9%E8%B1%A1\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u5bf9\u8c61<\/h3>\n<p>\u4e0a\u9762\u7684\u65e5\u671f\u4f8b\u5b50\uff0c\u901a\u8fc7 <strong>new<\/strong> \u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u65e5\u671f\u5bf9\u8c61\uff0c\u540c\u6837 <strong>new<\/strong> \u4e5f\u5141\u8bb8\u521b\u5efa\u4efb\u4f55\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u5b9e\u4f8b\u3002<\/p>\n<pre><code class=\"language-plain_text\">function Car(make, model, year) {\n    this.make = make;\n    this.model = model;\n    this.year = year;\n}\n\nconst myCar = new Car(&quot;BMW&quot;, &quot;X5&quot;, 2015);\nconst igorCar = new Car(&quot;Tesla&quot;, &quot;Model S&quot;, 2020);\nconst laurenCar = new Car(&quot;Ford&quot;, &quot;Escape&quot;, 2015);\n\nconsole.log(myCar); \/\/ Car { make: 'BMW', model: 'X5', year: 2015 }\nconsole.log(igorCar); \/\/ Car { make: 'Tesla', model: 'Model S', year: 2020 }\nconsole.log(laurenCar); \/\/ Car { make: 'Ford', model: 'Escape', year: 2015 }\n\nconsole.log(typeof myCar); \/\/ object\nconsole.log(myCar instanceof Car); \/\/ true\n<\/code><\/pre>\n<p>\u901a\u8fc7\u552f\u4e00\u7684 make \u3001model \u548c year \u521b\u5efa\u4e86\u4e09\u4e2a\u65b0\u7684\u72ec\u7acb\u7684\u5b9e\u4f8b\u3002\u56e0\u4e3a\u662f\u5bf9\u8c61\u6240\u4ee5 <strong>typeof<\/strong> \u90fd\u4f1a\u8f93\u51fa <strong>&#8216;object&#8217;<\/strong>\u3002\u5f53\u4f60\u8fdb\u884c <strong>instanceOf<\/strong> \u6d4b\u8bd5\u65f6\u4e5f\u4f1a\u8fd4\u56de true\u3002Car \u5bf9\u8c61\u5c31\u50cf\u662f myCar \u3001igorCar\u3001laurenCar \u7684\u84dd\u56fe\u6216\u662f\u6bcd\u4f53\u3002<\/p>\n<h3><a id=\"%E5%BD%93%E4%BD%BF%E7%94%A8new%E5%85%B3%E9%94%AE%E5%AD%97%E6%97%B6%EF%BC%8C%E5%88%B0%E5%BA%95%E5%8F%91%E7%94%9F%E4%BA%86%E4%BB%80%E4%B9%88%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5f53\u4f7f\u7528 new \u5173\u952e\u5b57\u65f6\uff0c\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48\uff1f<\/h3>\n<p>\u50cf\u4e0a\u9762\u7684\u4f8b\u5b50\uff0cCar \u53eb\u505a\u6784\u9020\u51fd\u6570\uff0c\u901a\u8fc7\u6211\u4eec\u5b9a\u4e49\u7684\u5c5e\u6027\u6765\u6784\u9020\u5bf9\u8c61\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u4f7f\u7528 new \u5173\u952e\u5b57\u65f6\uff0c\u4f1a\u7ecf\u5386\u4e0b\u9762\u56db\u6b65\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff1b<\/li>\n<li>\u8bbe\u7f6e\u7a7a\u5bf9\u8c61\u7684 prototype \u5c5e\u6027\u4e3a\u6784\u9020\u51fd\u6570\u7684 prototype \u5c5e\u6027\uff1b<\/li>\n<li>\u628a\u8bbe\u7f6e\u5230 this \u4e0a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u90fd\u7ed1\u5b9a\u5230\u7a7a\u5bf9\u8c61\u4e0a\uff1b<\/li>\n<li>\u8fd4\u56de\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\uff1b<\/li>\n<\/ol>\n<p>MDN \u5bf9 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/new#description\" target=\"_blank\" rel=\"noopener\">new<\/a> \u7684\u6b65\u9aa4\u8bf4\u660e\u3002<\/p>\n<h3><a id=\"%E4%BA%86%E8%A7%A3new%E5%85%B3%E9%94%AE%E5%AD%97%E4%B8%BA%E4%BD%95%E5%A6%82%E6%AD%A4%E9%87%8D%E8%A6%81%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e86\u89e3 new \u5173\u952e\u5b57\u4e3a\u4f55\u5982\u6b64\u91cd\u8981\uff1f<\/h3>\n<p>\u5728 JavaScript \u4e2d new \u3001classes \u3001objects\u3001this\u3001prototype \u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u57fa\u7840\u3002\u4f60\u53ef\u80fd\u4f1a\u542c\u8fc7 OOP\u6216\u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u5b83\u4fe9\u6ca1\u6709\u597d\u574f\u4e4b\u5206\u53ea\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u7f16\u7801\u51c6\u5219\u3002\u77e5\u9053\u8fd9\u4e9b\u539f\u5219\u4f1a\u8ba9\u4f60\u660e\u767d\u4e24\u79cd\u4e0d\u540c\u98ce\u683c\u7684\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u4ee5\u53ca\u5404\u81ea\u7684\u4f18\u52bf\u548c\u52a3\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\uff1ahttps:\/\/dev.to\/bhagatparwinder\/the-new-keyword-&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249,2,20,3],"tags":[6,83],"class_list":["post-965","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-javascript","tag-new"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=965"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/965\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/965\/revisions\/966"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}