{"id":9934,"date":"2023-11-28T21:20:44","date_gmt":"2023-11-28T13:20:44","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9934"},"modified":"2023-11-30T19:46:59","modified_gmt":"2023-11-30T11:46:59","slug":"javascript-zhongjavascript-chuang-jian-dui-xiang-d","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/javascript-zhongjavascript-chuang-jian-dui-xiang-d\/","title":{"rendered":"JavaScript \u4e2d Javascript \u521b\u5efa\u5bf9\u8c61\u7684\u51e0\u79cd\u65b9\u5f0f\uff1f"},"content":{"rendered":"<p>\u5728JavaScript\u4e2d\uff0c\u6709\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\u53ef\u4ee5\u521b\u5efa\u5bf9\u8c61\uff1a<\/p>\n<h3><a id=\"1%E5%AF%B9%E8%B1%A1%E5%AD%97%E9%9D%A2%E9%87%8F%EF%BC%88-object-literal%EF%BC%89\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1. \u5bf9\u8c61\u5b57\u9762\u91cf\uff08Object Literal\uff09<\/h3>\n<p>\u4f7f\u7528\u5bf9\u8c61\u5b57\u9762\u91cf\u662f\u521b\u5efa\u5bf9\u8c61\u6700\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u82b1\u62ec\u53f7 <code>{}<\/code> \u6765\u5b9a\u4e49\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5728\u5176\u4e2d\u76f4\u63a5\u6dfb\u52a0\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-javascript\">let person = {\n  name: &quot;Alice&quot;,\n  age: 30,\n  greet: function() {\n    console.log(&quot;Hello!&quot;);\n  }\n};\n<\/code><\/pre>\n<h3><a id=\"2%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%EF%BC%88-constructor-functions%EF%BC%89\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2. \u6784\u9020\u51fd\u6570\uff08Constructor Functions\uff09<\/h3>\n<p>\u4f7f\u7528\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u5177\u6709\u76f8\u4f3c\u7ed3\u6784\u7684\u5bf9\u8c61\u3002\u901a\u8fc7\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u53ef\u4ee5\u521b\u5efa\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002<\/p>\n<pre><code class=\"language-javascript\">function Person(name, age) {\n  this.name = name;\n  this.age = age;\n  this.greet = function() {\n    console.log(&quot;Hello!&quot;);\n  };\n}\n\nlet person1 = new Person(&quot;Bob&quot;, 25);\nlet person2 = new Person(&quot;Eve&quot;, 35);\n<\/code><\/pre>\n<h3><a id=\"3-object-create\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3. Object.create()<\/h3>\n<p>\u4f7f\u7528 <code>Object.create()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff0c\u5e76\u6307\u5b9a\u8be5\u5bf9\u8c61\u7684\u539f\u578b\u5bf9\u8c61\uff08prototype\uff09\u3002<\/p>\n<pre><code class=\"language-javascript\">let personProto = {\n  greet: function() {\n    console.log(&quot;Hello!&quot;);\n  }\n};\n\nlet person = Object.create(personProto);\nperson.name = &quot;Carol&quot;;\nperson.age = 28;\n<\/code><\/pre>\n<h3><a id=\"4-class%EF%BC%88es6%E5%BC%95%E5%85%A5%E7%9A%84%E7%B1%BB%EF%BC%89\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>4. class\uff08ES6\u5f15\u5165\u7684\u7c7b\uff09<\/h3>\n<p>ES6\u5f15\u5165\u4e86\u7c7b\uff08class\uff09\u8bed\u6cd5\u7cd6\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u6e05\u6670\u3001\u7b80\u6d01\u5730\u521b\u5efa\u5bf9\u8c61\u7684\u65b9\u5f0f\u3002<\/p>\n<pre><code class=\"language-javascript\">class Person {\n  constructor(name, age) {\n    this.name = name;\n    this.age = age;\n  }\n\n  greet() {\n    console.log(&quot;Hello!&quot;);\n  }\n}\n\nlet person = new Person(&quot;David&quot;, 32);\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u662fJavaScript\u4e2d\u5e38\u89c1\u7684\u51e0\u79cd\u521b\u5efa\u5bf9\u8c61\u7684\u65b9\u5f0f\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u81ea\u5df1\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728JavaScript\u4e2d\uff0c\u6709\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\u53ef\u4ee5\u521b\u5efa\u5bf9\u8c61\uff1a 1. \u5bf9\u8c61\u5b57\u9762\u91cf\uff08Object Literal\uff09 \u4f7f [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[115],"tags":[],"class_list":["post-9934","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=9934"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9934\/revisions"}],"predecessor-version":[{"id":13692,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9934\/revisions\/13692"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9934"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}