{"id":1349,"date":"2022-07-02T22:42:44","date_gmt":"2022-07-02T14:42:44","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1349"},"modified":"2022-07-02T23:31:34","modified_gmt":"2022-07-02T15:31:34","slug":"51-js-zhong-de-leipublicprivate-he-protected","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1349","title":{"rendered":"51 &#8211; JS \u4e2d\u7684\u7c7b\uff1aPublic\u3001Private \u548c Protected"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/07\/16567730420029.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/classes-in-js-public-private-and-protected-1lok\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/classes-in-js-public-private-and-protected-1lok<\/a><\/p>\n<p>\u5373\u4f7f ES6 \u4e2d\u5f15\u5165\u4e86 <code>class<\/code> \u5173\u952e\u5b57\u5f88\u597d\u7684\u6a21\u62df\u4e86\u7c7b\u7684\u884c\u4e3a\u4ee5\u53ca\u4f7f\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u4f46 JavaScript \u4e2d\u7684\u7c7b\u7f3a\u5931\u4e86\u521b\u5efa\u516c\u5171\u3001\u79c1\u6709\u548c\u4fdd\u62a4\u6210\u5458\u7684\u80fd\u529b\u3002<\/p>\n<p>\u82e5\u4f60\u4e4b\u524d\u4f7f\u7528\u8fc7\u5176\u4ed6\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u80af\u5b9a\u77e5\u9053\u5185\u90e8\u548c\u5916\u90e8\u63a5\u53e3\u7684\u91cd\u8981\u6027\u3002\u5185\u90e8\u63a5\u53e3\u5f15\u7528\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u53ea\u80fd\u5728\u7c7b\u7684\u5185\u90e8\u83b7\u53d6\u3002\u76f8\u53cd\uff0c\u5916\u90e8\u63a5\u53e3\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u53ef\u4ee5\u5728\u5185\u5916\u90e8\u90fd\u53ef\u83b7\u53d6\u3002<\/p>\n<p>\u4e3b\u8981\u6709\u4e09\u4e2a\u5173\u952e\u5b57\u5728\u8d77\u4f5c\u7528\uff1apublic\u3001protected \u548c private\u3002<\/p>\n<ol>\n<li>public\uff1a\u7c7b\u7684\u6240\u6709\u6210\u5458\u90fd\u53ef\u4ee5\u88ab\u7c7b\u7684\u5b9e\u4f8b\u83b7\u53d6\u3002<\/li>\n<li>private\uff1a\u7c7b\u6210\u5458\u53ea\u80fd\u5728\u7c7b\u4e2d\u88ab\u8bbf\u95ee\u3002<\/li>\n<li>protected\uff1a\u7c7b\u6210\u5458\u5728\u7c7b\u4ee5\u53ca\u5b50\u7c7b\u4e2d\u53ef\u4ee5\u88ab\u8bbf\u95ee<\/li>\n<\/ol>\n<p><strong>\u5728 JavaScript \u4e2d protected \u5173\u952e\u5b57\u662f\u6700\u96be\u6a21\u62df\u7684\u3002<\/strong><\/p>\n<h2><a id=\"public\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>public<\/h2>\n<p>publick \u662f JavaScript \u4e2d\u9ed8\u8ba4\u7684\uff0c\u5982\u679c\u4ece\u5bf9\u8c61\u4e0a\u53ef\u4ee5\u83b7\u53d6\u7684\u4e1c\u897f\uff0c\u90a3\u4e5f\u53ef\u4ee5\u4ece\u5b83\u7684\u5b9e\u4f8b\u4e0a\u83b7\u53d6\u3002<\/p>\n<pre><code class=\"language-plain_text\">const myObject = {\n    name: &quot;Parwinder&quot;,\n    sayMyName: function () {\n        return this.name;\n    }\n}\n\nconsole.log(myObject.name); \/\/ Parwinder\nconsole.log(myObject.sayMyName()); \/\/ Parwinder\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u53ef\u4ee5\u83b7\u53d6\u5c5e\u6027\u548c\u65b9\u6cd5\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u95ee\u9898\uff0c\u82e5\u4f60\u66f4\u503e\u5411\u7c7b\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"language-plain_text\">class ObjectCreator {\n    name;\n\n    constructor(name) {\n        this.name = name;\n    }\n\n    sayMyName() {\n        return this.name;\n    }\n}\n\nconst myObject = new ObjectCreator(&quot;Parwinder&quot;);\nconsole.log(myObject.name); \/\/ Parwinder\nconsole.log(myObject.sayMyName()); \/\/ Parwinder\n<\/code><\/pre>\n<h2><a id=\"private\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>private<\/h2>\n<p>JavaScript \u4e2d\u6709\u5f88\u591a\u65b9\u6cd5\u521b\u5efa\u79c1\u6709\u53d8\u91cf\uff0c\u7b2c\u4e00\u4e2a\u662f\u95ed\u5305\uff1a<\/p>\n<pre><code class=\"language-plain_text\">function carMonitor() {\n    var speed = 0;\n\n    return {\n        accelerate: function () {\n            return speed++;\n        }\n    }\n}\n\nvar car = new carMonitor();\nvar redCar = new carMonitor()\nconsole.log(car.accelerate()); \/\/ 0\nconsole.log(car.accelerate()); \/\/ 1\nconsole.log(redCar.accelerate()); \/\/ 0\nconsole.log(redCar.accelerate()); \/\/ 1\nconsole.log(car.accelerate()); \/\/ 2\nconsole.log(redCar.accelerate()); \/\/ 2\nconsole.log(speed); \/\/ speed is not defined\n<\/code><\/pre>\n<p>car \u548c redCar \u5404\u81ea\u7ef4\u62a4\u5b83\u4eec\u81ea\u5df1\u7684 <code>speed<\/code> \u53d8\u91cf\u5e76\u4e14\u5916\u90e8\u65e0\u6cd5\u83b7\u53d6\u5b83\u3002\u5728\u6784\u9020\u51fd\u6570\u6216\u7c7b\u4e2d\uff0c\u6211\u4eec\u5f3a\u5236\u7528\u6237\u901a\u8fc7\u65b9\u6cd5\u6765\u83b7\u53d6\u5c5e\u6027\u800c\u4e0d\u662f\u76f4\u63a5\u8bfb\u5199\u3002\u8fd9\u4e5f\u5c31\u662f\u5982\u4f55\u5c01\u88c5\u4ee3\u7801\u3002<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528 <code>#<\/code> \u7b26\u53f7\u3002<\/p>\n<pre><code class=\"language-plain_text\">class ObjectCreator {\n    #meaningOfLife;\n\n    constructor(name) {\n        this.#meaningOfLife = 42;\n    }\n\n    returnMeaningOfLife() {\n        return this.#meaningOfLife;\n    }\n\n    #returnAMessage() {\n        return &quot;You will do great things in life&quot;;\n    }\n}\n\nconst myObject = new ObjectCreator(&quot;Parwinder&quot;);\nconsole.log(myObject.returnMeaningOfLife()); \/\/ 42\nconsole.log(myObject[&quot;#meaningOfLife&quot;]); \/\/ undefined\nconsole.log(myObject.#meaningOfLife); \/\/ SyntaxError\nconsole.log(myObject.#returnAMessage); \/\/ SyntaxError\n<\/code><\/pre>\n<p>\u4ece\u8bed\u8a00\u5c42\u9762\u5f3a\u5236\u5c01\u88c5\u4e86\u4ee3\u7801\uff0c\u5916\u90e8\u76f4\u63a5\u83b7\u53d6 <code>#<\/code> \u5f15\u7528\u7684\u5b57\u6bb5\u5219\u4f1a\u62a5\u9519\u3002public \u548c private \u5b57\u6bb5\u540c\u65f6\u5b58\u5728\u4e0d\u4f1a\u51b2\u7a81\uff0c\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u65e2\u53ef\u4ee5\u6709\u79c1\u6709\u7684 #meaningOfLife \u4e5f\u53ef\u4ee5\u6709\u516c\u5171\u7684 meaningOfLife\u3002<\/p>\n<p><strong>\u7c7b\u4e2d\u4f7f\u7528 <code>#<\/code> \u7b26\u53f7\u6765\u58f0\u660e\u79c1\u6709\u6210\u5458\u662f\u5728 ES2019\/ES10 \u4e2d\u5f15\u5165\u7684\u3002<\/strong><\/p>\n<h2><a id=\"protected\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>protected<\/h2>\n<p>\u5c31\u50cf\u6211\u524d\u9762\u8bf4\u7684\u5728 JavaScript \u4e2d protected \u662f\u4e09\u4e2a\u65b9\u6cd5\u4e2d\u6700\u96be\u5b9e\u73b0\u7684\u3002\u6211\u80fd\u60f3\u5230\u7684\u9014\u5f84\u662f\u901a\u8fc7\u53ea\u5b58\u5728 getter \u800c\u6ca1\u6709 setter \u7684\u65b9\u6cd5\u6765\u5b9e\u73b0 protected \u3002<\/p>\n<p>\u82e5\u4f60\u6709\u522b\u7684\u65b9\u6cd5\u5b9e\u73b0\uff0c\u8bf7\u5206\u4eab\u4e00\u4e0b\uff01<\/p>\n<pre><code class=\"language-plain_text\">class NameGenerator {\n    _name;\n\n    constructor(name) {\n        this._name = name;\n    }\n\n    get name() {\n        return this._name;\n    }\n}\n\nlet nameGenerator = new NameGenerator(&quot;John&quot;);\nconsole.log(`My name is ${nameGenerator.name}`); \/\/ My name is John\nnameGenerator.name = &quot;Jane&quot;; \/\/ Cannot assign to 'name' because it is a read-only property.\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/classes-in-js-&#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":[2,20,3],"tags":[168,173,172,171],"class_list":["post-1349","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-class","tag-private","tag-protected","tag-public"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1349","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=1349"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1349\/revisions"}],"predecessor-version":[{"id":1350,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1349\/revisions\/1350"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}