{"id":168922,"date":"2024-05-08T16:34:37","date_gmt":"2024-05-08T08:34:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/168922.html"},"modified":"2024-05-08T16:34:41","modified_gmt":"2024-05-08T08:34:41","slug":"javascript-%e9%a1%b9%e7%9b%ae%e7%a8%8b%e5%ba%8f%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-new-%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/168922.html","title":{"rendered":"JavaScript \u9879\u76ee\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f7f\u7528 new \u547d\u4ee4"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035231\/d657bb9c-f809-44e3-902d-067ef3d9120e.webp\" alt=\"JavaScript \u9879\u76ee\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f7f\u7528 new \u547d\u4ee4\" \/><\/p>\n<p><p><strong>JavaScript \u9879\u76ee\u7a0b\u5e8f\u4e2d\u4f7f\u7528 <code>new<\/code> \u547d\u4ee4\u4e3b\u8981\u662f\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u5bf9\u8c61\u7c7b\u578b\u7684\u5b9e\u4f8b\u6216\u5177\u6709\u6784\u9020\u51fd\u6570\u7684\u5185\u5efa\u5bf9\u8c61\u7c7b\u578b\u7684\u5b9e\u4f8b<\/strong>\u3002\u5728\u4f7f\u7528 <code>new<\/code> \u547d\u4ee4\u65f6\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f5c\u4e3a\u65b0\u5bf9\u8c61\u7684\u201c\u84dd\u56fe\u201d\uff0c\u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<strong>\u901a\u8fc7 <code>new<\/code> \u64cd\u4f5c\u7b26\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3001\u5206\u914d\u5185\u5b58\u7a7a\u95f4\u3001\u5c06 <code>this<\/code> \u7ed1\u5b9a\u5230\u65b0\u521b\u5efa\u7684\u5bf9\u8c61<\/strong> \u4e0a\uff0c\u5e76\u4e14\u5982\u679c\u6784\u9020\u51fd\u6570\u6ca1\u6709\u663e\u5f0f\u8fd4\u56de\u5176\u4ed6\u5bf9\u8c61\uff0c\u5219\u8fd4\u56de\u8be5\u65b0\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a <code>Person<\/code> \u7684\u6784\u9020\u51fd\u6570\uff0c\u5b83\u63a5\u6536 <code>name<\/code> \u548c <code>age<\/code> \u4e24\u4e2a\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>new<\/code> \u64cd\u4f5c\u7b26\u6765\u521b\u5efa <code>Person<\/code> \u7684\u65b0\u5b9e\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Person(name, age) {<\/p>\n<p>    this.name = name;<\/p>\n<p>    this.age = age;<\/p>\n<p>    this.greet = function() {<\/p>\n<p>        console.log(&#039;Hello, my name is &#039; + this.name + &#039; and I am &#039; + this.age + &#039; years old.&#039;);<\/p>\n<p>    };<\/p>\n<p>}<\/p>\n<p>\/\/ \u4f7f\u7528 new \u547d\u4ee4\u521b\u5efa Person \u7684\u4e00\u4e2a\u5b9e\u4f8b<\/p>\n<p>var person1 = new Person(&#039;Alice&#039;, 30);<\/p>\n<p>person1.greet(); \/\/ \u8f93\u51fa: Hello, my name is Alice and I am 30 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u6784\u9020\u51fd\u6570\u548c <code>new<\/code> \u547d\u4ee4\u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u662f\u666e\u901a\u51fd\u6570\uff0c\u4f46\u5b83\u4eec\u7684\u7528\u9014\u662f\u80fd\u591f\u4f7f\u7528 <code>new<\/code> \u547d\u4ee4\u6765\u521b\u5efa\u5bf9\u8c61\u3002\u4e00\u4e2a\u597d\u7684\u5b9e\u8df5\u662f\u7528\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u8868\u793a\u4e00\u4e2a\u51fd\u6570\u662f\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u521b\u5efa\u6784\u9020\u51fd\u6570<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u901a\u5e38\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u5b9a\u4e49\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function MyObject(param1, param2) {<\/p>\n<p>    this.property1 = param1;<\/p>\n<p>    this.property2 = param2;<\/p>\n<p>    this.method1 = function() {<\/p>\n<p>        \/\/ \u65b9\u6cd5\u7684\u5177\u4f53\u903b\u8f91<\/p>\n<p>    };<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>new<\/code> \u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u6bcf\u5f53\u4f7f\u7528 <code>new<\/code> \u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u5bf9\u8c61\u65f6\uff0cJavaScript \u4f1a\u81ea\u52a8\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u7b80\u5355JavaScript\u5bf9\u8c61\uff08\u5373 <code>{}<\/code>\uff09\u3002<\/li>\n<li>\u94fe\u63a5\u8be5\u5bf9\u8c61\uff08\u8bbe\u5176 <code>__proto__<\/code> \u5c5e\u6027\uff09\u81f3\u53e6\u4e00\u4e2a\u5bf9\u8c61\u3002<\/li>\n<li>\u5c06\u6b65\u9aa41\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u4f5c\u4e3a <code>this<\/code> \u7684\u4e0a\u4e0b\u6587\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u663e\u5f0f\u5730\u8fd4\u56de\u5176\u4ed6\u5bf9\u8c61\uff0c\u5219\u8fd4\u56de <code>this<\/code>\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001<code>new<\/code> \u547d\u4ee4\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><h4><strong>\u5185\u5efa\u5bf9\u8c61\u548c\u81ea\u5b9a\u4e49\u5bf9\u8c61<\/strong>\uff1a<\/h4>\n<\/p>\n<p><p><code>new<\/code> \u4e5f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5185\u5efa\u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u6bd4\u5982 <code>Date<\/code>\u3001<code>RegExp<\/code>\u3001<code>String<\/code>\u3001<code>Number<\/code> \u7b49\u3002<\/p>\n<\/p>\n<p><h4><strong>\u8fd4\u56de\u5176\u4ed6\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570<\/strong>\uff1a<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6784\u9020\u51fd\u6570\u8fd4\u56de\u7684\u662f\u53e6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u90a3\u4e48 <code>new<\/code> \u547d\u4ee4\u7684\u7ed3\u679c\u5c31\u4f1a\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u539f\u6765\u610f\u56fe\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001<code>new<\/code> \u547d\u4ee4\u548c\u5de5\u5382\u6a21\u5f0f\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u6a21\u5f0f\u4f7f\u7528 <code>new<\/code> \u64cd\u4f5c\u7b26\u521b\u5efa\u65b0\u5bf9\u8c61\u5e76\u663e\u5f0f\u5730\u5b9a\u4e49\u5176\u539f\u578b\u94fe\u548c\u5c5e\u6027\uff0c\u800c\u5de5\u5382\u6a21\u5f0f\u5219\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u521b\u5efa\u5bf9\u8c61\uff0c\u6ca1\u6709\u663e\u5f0f\u4f7f\u7528 <code>new<\/code> \u64cd\u4f5c\u7b26\uff0c\u4e14\u6ca1\u6709\u663e\u5f0f\u8bbe\u7f6e\u5bf9\u8c61\u539f\u578b\u3002<\/p>\n<\/p>\n<p><p><strong>\u6784\u9020\u51fd\u6570\u6a21\u5f0f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Car(model, year) {<\/p>\n<p>    this.model = model;<\/p>\n<p>    this.year = year;<\/p>\n<p>    this.displayInfo = function() {<\/p>\n<p>        console.log(this.model + &quot; &quot; + this.year);<\/p>\n<p>    };<\/p>\n<p>}<\/p>\n<p>var myCar = new Car(&quot;Toyota&quot;, 2020);<\/p>\n<p>myCar.displayInfo();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5de5\u5382\u6a21\u5f0f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function createCar(model, year) {<\/p>\n<p>    var car = {};<\/p>\n<p>    car.model = model;<\/p>\n<p>    car.year = year;<\/p>\n<p>    car.displayInfo = function() {<\/p>\n<p>        console.log(this.model + &quot; &quot; + this.year);<\/p>\n<p>    };<\/p>\n<p>    return car;<\/p>\n<p>}<\/p>\n<p>var myCar = createCar(&quot;Toyota&quot;, 2020);<\/p>\n<p>myCar.displayInfo();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001<code>new.target<\/code> \u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5185\u90e8\uff0c<code>new.target<\/code> \u5c5e\u6027\u5141\u8bb8\u4f60\u68c0\u6d4b\u8be5\u51fd\u6570\u662f\u5426\u662f\u901a\u8fc7 <code>new<\/code> \u64cd\u4f5c\u7b26\u88ab\u8c03\u7528\u7684\u3002\u5728\u7528 <code>new<\/code> \u8c03\u7528\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u65f6\uff0c<code>new.target<\/code> \u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u6784\u9020\u51fd\u6570\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Foo() {<\/p>\n<p>    if (!new.target) throw &#039;Foo() must be called with new&#039;;<\/p>\n<p>    console.log(&#039;Foo instantiated with new&#039;);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001<code>new<\/code> \u547d\u4ee4\u7684\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u6df1\u5165\u4e86\u89e3 <code>new<\/code> \u64cd\u4f5c\u7b26\u7684\u5de5\u4f5c\u539f\u7406\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u7f16\u5199\u548c\u7406\u89e3\u5bf9\u8c61\u521b\u5efa\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u7a7a\u5bf9\u8c61<\/strong>\uff1a\u5728\u4f7f\u7528 <code>new<\/code> \u65f6\uff0c\u5b83\u4f1a\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u6784\u9020\u51fd\u6570\u4e0a\u4e0b\u6587<\/strong>\uff1a\u5c06\u6784\u9020\u51fd\u6570\u7684 <code>this<\/code> \u6307\u5411\u8fd9\u4e2a\u65b0\u521b\u5efa\u7684\u7a7a\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u7ee7\u627f\u539f\u578b<\/strong>\uff1a\u65b0\u5bf9\u8c61\u4f1a\u7ee7\u627f\u6784\u9020\u51fd\u6570\u539f\u578b\u4e0a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u8fd4\u56de\u5bf9\u8c61<\/strong>\uff1a\u5982\u679c\u6784\u9020\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u51fd\u6570\u6216\u8005\u5bf9\u8c61\uff0c<code>new<\/code> \u64cd\u4f5c\u7b26\u4f1a\u8fd4\u56de\u6784\u9020\u51fd\u6570\u4fee\u6539\u7684 <code>this<\/code> \u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001<code>new<\/code> \u547d\u4ee4\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u4f7f\u7528 <code>new<\/code> \u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u5e94\u8be5\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6784\u9020\u51fd\u6570\u7684\u547d\u540d\u89c4\u8303<\/strong>\uff1a\u5e94\u4f7f\u7528\u9996\u5b57\u6bcd\u5927\u5199\u7684\u9a7c\u5cf0\u547d\u540d\u6cd5\u6765\u547d\u540d\u6784\u9020\u51fd\u6570\u3002<\/li>\n<li><strong><code>new<\/code> \u64cd\u4f5c\u7b26\u5fc5\u987b\u548c\u6784\u9020\u51fd\u6570\u4e00\u8d77\u4f7f\u7528<\/strong>\uff1a\u5426\u5219\u51fd\u6570\u5185\u7684 <code>this<\/code> \u5c06\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff08\u5728\u6d4f\u89c8\u5668\u4e2d\u662f <code>window<\/code>\uff09\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7684\u5168\u5c40\u53d8\u91cf\u6c61\u67d3\u3002<\/li>\n<li><strong>\u6784\u9020\u51fd\u6570\u548c\u666e\u901a\u51fd\u6570\u7684\u533a\u5206<\/strong>\uff1a\u6784\u9020\u51fd\u6570\u9664\u4e86\u547d\u540d\u4e0d\u540c\uff0c\u7406\u8bba\u4e0a\u548c\u666e\u901a\u51fd\u6570\u6ca1\u6709\u5dee\u522b\uff0c\u4f46\u6700\u597d\u4fdd\u6301\u6784\u9020\u51fd\u6570\u7528\u6765\u6784\u9020\u5bf9\u8c61\u7684\u7528\u9014\uff0c\u666e\u901a\u51fd\u6570\u7528\u6765\u6267\u884c\u5177\u4f53\u903b\u8f91\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u603b\u7ed3\u548c\u5b9e\u8df5\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728 JavaScript \u4e2d\uff0c<code>new<\/code> \u547d\u4ee4\u662f\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u7279\u5b9a\u7ed3\u6784\u548c\u65b9\u6cd5\u7684\u65b0\u5bf9\u8c61\u3002\u7406\u89e3 <code>new<\/code> \u7684\u5de5\u4f5c\u539f\u7406\u548c\u4f7f\u7528\u573a\u666f\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u3001\u6e05\u6670\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u5b9e\u8df5\u4e2d\u5e94\u6ce8\u610f\u6784\u9020\u51fd\u6570\u7684\u6b63\u786e\u4f7f\u7528\u548c <code>new<\/code> \u64cd\u4f5c\u7b26\u4e0e\u4e4b\u914d\u5408\u7684\u5408\u7406\u5e94\u7528\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u53d8\u91cf\u548c\u6f5c\u5728\u7684\u5185\u5b58\u6cc4\u9732\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8ba8\u8bba\uff0c\u4f60\u73b0\u5728\u5e94\u8be5\u66f4\u6e05\u695a\u5982\u4f55\u5728 JavaScript \u9879\u76ee\u4e2d\u6709\u6548\u5730\u4f7f\u7528 <code>new<\/code> \u547d\u4ee4\uff0c\u4ee5\u53ca\u5982\u4f55\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6a21\u5f0f\u521b\u5efa\u53ef\u91cd\u7528\u548c\u7ec4\u7ec7\u6027\u5f3a\u7684\u4ee3\u7801\u7ed3\u6784\u3002\u5728\u5b9e\u8df5\u4f7f\u7528\u4e2d\uff0c\u603b\u662f\u786e\u4fdd\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u4fdd\u6301\u4ee3\u7801\u7684\u4e00\u81f4\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5728 JavaScript \u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 new \u547d\u4ee4\u521b\u5efa\u5bf9\u8c61\uff1f<\/strong><br \/>\nA: \u5728 JavaScript \u9879\u76ee\u4e2d\uff0c\u4f7f\u7528 new \u547d\u4ee4\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u4f7f\u7528 new \u547d\u4ee4\u3002<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u6784\u9020\u51fd\u6570\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u6a21\u677f\uff0c\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a Person \u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u4eba\u7684\u5b9e\u4f8b\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">function Person(name, age) {\n  this.name = name;\n  this.age = age;\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u7136\u540e\uff0c\u4f7f\u7528 new \u547d\u4ee4\u6765\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 Person \u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a John\uff0c\u5e74\u9f84\u4e3a 25 \u7684\u4eba\u7684\u5b9e\u4f8b\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">var john = new Person(&quot;John&quot;, 25);\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cnew Person(&quot;John&quot;, 25) \u5c06\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Person \u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9 john \u53d8\u91cf\u3002<\/p>\n<ol start=\"3\">\n<li>\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5b9e\u4f8b\u7684\u5c5e\u6027\u6765\u64cd\u4f5c\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 john.name \u548c john.age \u6765\u83b7\u53d6 john \u5bf9\u8c61\u7684\u540d\u79f0\u548c\u5e74\u9f84\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">console.log(john.name); \/\/ \u8f93\u51fa\uff1aJohn\nconsole.log(john.age); \/\/ \u8f93\u51fa\uff1a25\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728 JavaScript \u9879\u76ee\u4e2d\u4f7f\u7528 new \u547d\u4ee4\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/p>\n<p><strong>Q: JavaScript \u9879\u76ee\u4e2d\u5982\u4f55\u6b63\u786e\u4f7f\u7528 new \u547d\u4ee4\u6765\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\uff1f<\/strong><br \/>\nA: \u5728 JavaScript \u9879\u76ee\u4e2d\uff0c\u4f7f\u7528 new \u547d\u4ee4\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u3002\u4e3a\u4e86\u6b63\u786e\u4f7f\u7528 new \u547d\u4ee4\u6765\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u8be5\u6784\u9020\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u5b9e\u4f8b\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a Animal \u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u52a8\u7269\u7684\u5b9e\u4f8b\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">function Animal(name, age) {\n  this.name = name;\n  this.age = age;\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u7136\u540e\uff0c\u4f7f\u7528 new \u547d\u4ee4\u591a\u6b21\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 Animal \u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u591a\u4e2a\u52a8\u7269\u7684\u5b9e\u4f8b\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">var cat = new Animal(&quot;Fluffy&quot;, 3);\nvar dog = new Animal(&quot;Buddy&quot;, 5);\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0ccat \u5c06\u4f1a\u662f\u4e00\u4e2a\u540d\u4e3a &quot;Fluffy&quot; \u7684\u732b\u7684\u5b9e\u4f8b\uff0c\u800c dog \u5c06\u4f1a\u662f\u4e00\u4e2a\u540d\u4e3a &quot;Buddy&quot; \u7684\u72d7\u7684\u5b9e\u4f8b\u3002<\/p>\n<ol start=\"3\">\n<li>\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5b9e\u4f8b\u7684\u5c5e\u6027\u6765\u64cd\u4f5c\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 cat.name \u548c cat.age \u6765\u83b7\u53d6 cat \u5bf9\u8c61\u7684\u540d\u79f0\u548c\u5e74\u9f84\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">console.log(cat.name); \/\/ \u8f93\u51fa\uff1aFluffy\nconsole.log(cat.age); \/\/ \u8f93\u51fa\uff1a3\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728 JavaScript \u9879\u76ee\u4e2d\u6b63\u786e\u4f7f\u7528 new \u547d\u4ee4\u6765\u521b\u5efa\u5e76\u64cd\u4f5c\u591a\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p><strong>Q: JavaScript \u9879\u76ee\u4e2d\u5982\u4f55\u4f7f\u7528 new \u547d\u4ee4\u8c03\u7528\u4e00\u4e2a\u5df2\u6709\u7684\u6784\u9020\u51fd\u6570\uff1f<\/strong><br \/>\nA: \u5728 JavaScript \u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 new \u547d\u4ee4\u8c03\u7528\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u5bf9\u8c61\u5b9e\u4f8b\u3002\u4e3a\u4e86\u4f7f\u7528 new \u547d\u4ee4\u8c03\u7528\u5df2\u6709\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b9a\u4e49\u4e86\u9700\u8981\u8c03\u7528\u7684\u6784\u9020\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a Person \u7684\u6784\u9020\u51fd\u6570\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">function Person(name, age) {\n  this.name = name;\n  this.age = age;\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u7136\u540e\uff0c\u4f7f\u7528 new \u547d\u4ee4\u6765\u8c03\u7528\u5df2\u6709\u7684\u6784\u9020\u51fd\u6570\u5e76\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 new Person() \u6765\u8c03\u7528 Person \u6784\u9020\u51fd\u6570\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">var john = new Person(&quot;John&quot;, 25);\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cnew Person(&quot;John&quot;, 25) \u5c06\u4f1a\u8c03\u7528 Person \u6784\u9020\u51fd\u6570\u5e76\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a John\uff0c\u5e74\u9f84\u4e3a 25 \u7684\u4eba\u7684\u5b9e\u4f8b\u3002<\/p>\n<ol start=\"3\">\n<li>\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5b9e\u4f8b\u7684\u5c5e\u6027\u6765\u64cd\u4f5c\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 john.name \u548c john.age \u6765\u83b7\u53d6 john \u5bf9\u8c61\u7684\u540d\u79f0\u548c\u5e74\u9f84\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">console.log(john.name); \/\/ \u8f93\u51fa\uff1aJohn\nconsole.log(john.age); \/\/ \u8f93\u51fa\uff1a25\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728 JavaScript \u9879\u76ee\u4e2d\u4f7f\u7528 new \u547d\u4ee4\u8c03\u7528\u5df2\u6709\u7684\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u9879\u76ee\u7a0b\u5e8f\u4e2d\u4f7f\u7528 new \u547d\u4ee4\u4e3b\u8981\u662f\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u5bf9\u8c61\u7c7b\u578b\u7684\u5b9e\u4f8b\u6216\u5177\u6709\u6784\u9020\u51fd\u6570\u7684\u5185 [&hellip;]","protected":false},"author":3,"featured_media":168926,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168922"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=168922"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168922\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/168926"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=168922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=168922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=168922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}