{"id":3270,"date":"2024-11-10T16:18:00","date_gmt":"2024-11-10T08:18:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3270\/"},"modified":"2024-11-10T16:18:00","modified_gmt":"2024-11-10T08:18:00","slug":"%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9cjs%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3270\/","title":{"rendered":"\u5982\u4f55\u5236\u4f5cjs\u6a21\u578b"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3001\u5de5\u5382\u51fd\u6570\u6216\u7c7b\u6765\u521b\u5efa javascript \u6a21\u578b\uff0c\u4ece\u800c\u5b58\u50a8\u3001\u7ba1\u7406\u548c\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u6570\u636e\u3002\u6a21\u578b\u7684\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7\u70b9\u6216\u65b9\u62ec\u53f7\u8fd0\u7b97\u7b26\u8fdb\u884c\u8bbf\u95ee\u548c\u4fee\u6539\uff0c\u8fd8\u53ef\u4ee5\u5305\u542b\u7528\u4e8e\u6267\u884c\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110808512984075.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5236\u4f5cjs\u6a21\u578b\u63d2\u56fe\" alt=\"\u5982\u4f55\u5236\u4f5cjs\u6a21\u578b\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5236\u4f5c JavaScript \u6a21\u578b<\/strong><\/p>\n<p>JavaScript \u6a21\u578b\u662f\u4e00\u79cd\u7528\u4e8e\u4ee3\u8868\u548c\u64cd\u4f5c\u6570\u636e\u7684 JavaScript \u5bf9\u8c61\u3002\u5b83\u4eec\u5e7f\u6cdb\u7528\u4e8e\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7528\u4e8e\u5b58\u50a8\u3001\u7ba1\u7406\u548c\u5904\u7406\u6570\u636e\u3002<\/p>\n<p><strong>1. \u6784\u9020\u51fd\u6570<\/strong><\/p>\n<p>\u521b\u5efa JavaScript \u6a21\u578b\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3002\u6784\u9020\u51fd\u6570\u662f\u4e00\u4e2a\u7279\u6b8a\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u6784\u9020\u51fd\u6570\u521b\u5efa\u6a21\u578b\uff1a<\/p>\n<pre>function Person(name, age) {\n  this.name = name;\n  this.age = age;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236     <\/p>\n<pre>const person = new Person(\"John\", 30);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u5de5\u5382\u51fd\u6570<\/strong><\/p>\n<p>\u5de5\u5382\u51fd\u6570\u662f\u4e00\u79cd\u521b\u5efa\u5bf9\u8c61\u7684\u66ff\u4ee3\u65b9\u6cd5\u3002\u5b83\u4eec\u8fd4\u56de\u4e00\u4e2a\u65b0\u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684 new \u8fd0\u7b97\u7b26\u3002<\/p>\n<pre>function createPerson(name, age) {\n  return {\n    name: name,\n    age: age\n  };\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<pre>const person = createPerson(\"John\", 30);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u7c7b<\/strong><\/p>\n<p>ES6 \u5f15\u5165\u4e86\u7c7b\uff0c\u4e3a JavaScript \u63d0\u4f9b\u4e86\u4e00\u79cd\u7c7b\u4f3c\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\u3002\u7c7b\u4f7f\u7528 class \u5173\u952e\u5b57\u5b9a\u4e49\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>class Person {\n  constructor(name, age) {\n    this.name = name;\n    this.age = age;\n  }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236     <\/p>\n<pre>const person = new Person(\"John\", 30);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u8bbf\u95ee\u548c\u4fee\u6539\u5c5e\u6027<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u70b9\u8fd0\u7b97\u7b26\u6216\u65b9\u62ec\u53f7\u8fd0\u7b97\u7b26\u8bbf\u95ee\u548c\u4fee\u6539\u6a21\u578b\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>person.name \/\/ \"John\"\nperson.age = 31<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. \u65b9\u6cd5<\/strong><\/p>\n<p>\u6a21\u578b\u8fd8\u53ef\u4ee5\u5305\u542b\u7528\u4e8e\u6267\u884c\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u5bf9\u8c61\u5b9a\u4e49\u4e2d\u4f5c\u4e3a\u5c5e\u6027\u5b9a\u4e49\u3002<\/p>\n<pre>class Person {\n  constructor(name, age) {\n    this.name = name;\n    this.age = age;\n  }\n  \n  greet() {\n    return `Hello, my name is ${this.name} and I am ${this.age} years old.`;\n  }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<pre>const person = new Person(\"John\", 30);\nperson.greet() \/\/ \"Hello, my name is John and I am 30 years old.\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3001\u5de5\u5382\u51fd\u6570\u6216\u7c7b\uff0c\u4ee5\u53ca\u8bbf\u95ee\u5668\u548c\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u64cd\u4f5c JavaScript \u6a21\u578b\uff0c\u4ece\u800c\u6709\u6548\u5730\u5b58\u50a8\u3001\u7ba1\u7406\u548c\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5236\u4f5cjs\u6a21\u578b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3001\u5de5\u5382\u51fd\u6570\u6216\u7c7b\u6765\u521b\u5efa javascript \u6a21\u578b\uff0c\u4ece\u800c\u5b58\u50a8\u3001\u7ba1\u7406\u548c\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u6570\u636e\u3002\u6a21\u578b\u7684\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7\u70b9\u6216\u65b9\u62ec\u53f7\u8fd0\u7b97\u7b26\u8fdb\u884c\u8bbf\u95ee\u548c\u4fee\u6539\uff0c\u8fd8\u53ef\u4ee5\u5305\u542b\u7528\u4e8e\u6267\u884c\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002 \u5982\u4f55\u5236\u4f5c JavaScript \u6a21\u578b JavaScript \u6a21\u578b\u662f\u4e00\u79cd\u7528\u4e8e\u4ee3\u8868\u548c\u64cd\u4f5c\u6570\u636e\u7684 JavaScript \u5bf9\u8c61\u3002\u5b83\u4eec\u5e7f\u6cdb\u7528\u4e8e\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7528\u4e8e\u5b58\u50a8\u3001\u7ba1\u7406\u548c\u5904\u7406\u6570\u636e\u3002 1. \u6784\u9020\u51fd\u6570 \u521b\u5efa JavaScript \u6a21\u578b\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3002\u6784\u9020\u51fd\u6570\u662f\u4e00\u4e2a\u7279\u6b8a\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u6784\u9020\u51fd\u6570\u521b\u5efa\u6a21\u578b\uff1a function Person(name, age) { this.name = name; this.age = age; } \u767b\u5f55\u540e\u590d\u5236 const person = new Person(&#8220;John&#8221;, 30); \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 2. \u5de5\u5382\u51fd\u6570 \u5de5\u5382\u51fd\u6570\u662f\u4e00\u79cd\u521b\u5efa\u5bf9\u8c61\u7684\u66ff\u4ee3\u65b9\u6cd5\u3002\u5b83\u4eec\u8fd4\u56de\u4e00\u4e2a\u65b0\u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684 new \u8fd0\u7b97\u7b26\u3002 function createPerson(name, age) { return { name: name, age: age }; } \u767b\u5f55\u540e\u590d\u5236 const person [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3270","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3270"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3270\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}