{"id":209,"date":"2026-01-27T15:50:00","date_gmt":"2026-01-27T07:50:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=209"},"modified":"2026-06-04T15:53:09","modified_gmt":"2026-06-04T07:53:09","slug":"%e5%b1%9e%e6%80%a7%e6%8f%8f%e8%bf%b0%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/01\/27\/%e5%b1%9e%e6%80%a7%e6%8f%8f%e8%bf%b0%e7%ac%a6\/","title":{"rendered":"\u5c5e\u6027\u63cf\u8ff0\u7b26"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5c5e\u6027\u63cf\u8ff0\u7b26\u8be6\u89e3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <em>JavaScript<\/em> \u4e2d\uff0c\u5bf9\u8c61\u7684\u5c5e\u6027\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6570\u636e\u5c5e\u6027\uff1a\u5b83\u7684\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u6570\u636e<\/li>\n\n\n\n<li>\u5b58\u53d6\u5668\u5c5e\u6027\uff1a\u5b83\u7684\u672c\u8d28\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u4f46\u662f\u53ef\u4ee5\u5c06\u5b83\u5f53\u4f5c\u666e\u901a\u5c5e\u6027\u6765\u4f7f\u7528\uff0c\u5f53\u7ed9\u8be5\u5c5e\u6027\u8d4b\u503c\u65f6\uff0c\u4f1a\u8fd0\u884c\u76f8\u5e94\u7684 <em>setter<\/em> \u51fd\u6570\uff0c\u5f53\u83b7\u53d6\u8be5\u5c5e\u6027\u7684\u503c\u65f6\uff0c\u4f1a\u8fd0\u884c\u76f8\u5e94\u7684 <em>getter<\/em> \u51fd\u6570\u3002\u9664\u4e86\u5b58\u53d6\u5668\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u5173\u952e\u5b57\uff0c\u7528\u4ee5\u8868\u793a\u5f53\u524d\u5c5e\u6027\u662f\u5426\u53ef\u5199\u3001\u662f\u5426\u6709\u9ed8\u8ba4\u503c\u3001\u662f\u5426\u53ef\u679a\u4e3e\u7b49\uff0c\u8fd9\u4e9b\u5173\u952e\u5b57\u5c31\u662f\u5c5e\u6027\u63cf\u8ff0\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c5e\u6027\u63cf\u8ff0\u7b26\u662f <em>ECMAScript<\/em> 5 \u65b0\u589e\u7684\u8bed\u6cd5\uff0c\u5b83\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u5185\u90e8\u5bf9\u8c61\uff0c\u7528\u6765\u63cf\u8ff0\u5bf9\u8c61\u7684\u5c5e\u6027\u7684\u7279\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c5e\u6027\u63cf\u8ff0\u7b26\u7684\u7ed3\u6784<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b9a\u4e49\u5bf9\u8c61\u3001\u5b9a\u4e49\u5c5e\u6027\u65f6\uff0c\u6211\u4eec\u66fe\u7ecf\u4ecb\u7ecd\u8fc7\u5c5e\u6027\u63cf\u8ff0\u7b26\uff0c\u5c5e\u6027\u63cf\u8ff0\u7b26\u5b9e\u9645\u4e0a\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c5e\u6027\u63cf\u8ff0\u7b26\u4e00\u5171\u6709 <em>6<\/em> \u4e2a\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>value<\/em>\uff1a\u8bbe\u7f6e\u5c5e\u6027\u503c\uff0c\u9ed8\u8ba4\u503c\u4e3a <em>undefined<\/em>\u3002<\/li>\n\n\n\n<li><em>writable<\/em>\uff1a\u8bbe\u7f6e\u5c5e\u6027\u503c\u662f\u5426\u53ef\u5199\uff0c\u9ed8\u8ba4\u503c\u4e3a <em>false<\/em>\u3002<\/li>\n\n\n\n<li><em>enumerable<\/em>\uff1a\u8bbe\u7f6e\u5c5e\u6027\u662f\u5426\u53ef\u679a\u4e3e\uff0c\u5373\u662f\u5426\u5141\u8bb8\u4f7f\u7528 <em>for\/in<\/em> \u8bed\u53e5\u6216 <em>Object.keys( )<\/em> \u51fd\u6570\u904d\u5386\u8bbf\u95ee\uff0c\u9ed8\u8ba4\u4e3a <em>false<\/em>\u3002<\/li>\n\n\n\n<li><em>configurable<\/em>\uff1a\u5f53\u8bbe\u7f6e\u4e3a false \u65f6\uff0c\u8be5\u5c5e\u6027\u7684\u7c7b\u578b\u4e0d\u80fd\u5728\u6570\u636e\u5c5e\u6027\u548c\u8bbf\u95ee\u5668\u5c5e\u6027\u4e4b\u95f4\u66f4\u6539\uff0c\u4e14\u8be5\u5c5e\u6027\u4e0d\u53ef\u88ab\u5220\u9664\uff0c\u4e14\u5176\u63cf\u8ff0\u7b26\u7684\u5176\u4ed6\u5c5e\u6027\u4e5f\u4e0d\u80fd\u88ab\u66f4\u6539\uff08\u4f46\u662f\uff0c\u5982\u679c\u5b83\u662f\u4e00\u4e2a\u53ef\u5199\u7684\u6570\u636e\u63cf\u8ff0\u7b26\uff0c\u5219 value \u53ef\u4ee5\u88ab\u66f4\u6539\uff0cwritable \u53ef\u4ee5\u66f4\u6539\u4e3a false\uff09\u3002\u9ed8\u8ba4\u503c\u4e3a false\u3002<\/li>\n\n\n\n<li><em>get<\/em>\uff1a\u53d6\u503c\u51fd\u6570\uff0c\u9ed8\u8ba4\u4e3a <em>undefined<\/em>\u3002<\/li>\n\n\n\n<li><em>set<\/em>\uff1a\u5b58\u503c\u51fd\u6570\uff0c\u9ed8\u8ba4\u4e3a <em>undefined<\/em>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\u8fd9\u51e0\u4e2a\u5c5e\u6027\u4e0d\u662f\u90fd\u53ef\u4ee5\u4e00\u8d77\u8bbe\u7f6e\uff0c\u5177\u4f53\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/xiejie-typora.oss-cn-chengdu.aliyuncs.com\/2021-10-21-031647.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b 1<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u793a\u4f8b\u6f14\u793a\u4e86\u4f7f\u7528 <em>value<\/em> \u8bfb\u5199\u5c5e\u6027\u503c\u7684\u57fa\u672c\u7528\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {}; &nbsp;\/\/\u5b9a\u4e49\u7a7a\u5bf9\u8c61<br>Object.defineProperty(obj, 'x', {value : 100}); &nbsp;\/\/\u6dfb\u52a0\u5c5e\u6027x\uff0c\u503c\u4e3a100<br>console.log(Object.getOwnPropertyDescriptor(obj, 'x').value); &nbsp;\/\/\u8fd4\u56de100<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b 2<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u793a\u4f8b\u6f14\u793a\u4e86\u4f7f\u7528 <em>writable<\/em> \u5c5e\u6027\u7981\u6b62\u4fee\u6539\u5c5e\u6027 <em>x<\/em>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {};<br>Object.defineProperty(obj, 'x', {<br> &nbsp; &nbsp;value : 1, &nbsp;\/\/\u8bbe\u7f6e\u5c5e\u6027\u9ed8\u8ba4\u503c\u4e3a1<br> &nbsp; &nbsp;writable : false &nbsp;\/\/\u7981\u6b62\u4fee\u6539\u5c5e\u6027\u503c<br>});<br>obj.x = 2; &nbsp;\/\/\u4fee\u6539\u5c5e\u6027x\u7684\u503c<br>console.log(obj.x); &nbsp;\/\/ 1 \u8bf4\u660e\u4fee\u6539\u5931\u8d25<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b63\u5e38\u6a21\u5f0f\u4e0b\uff0c\u5982\u679c <em>writable<\/em> \u4e3a <em>false<\/em>\uff0c\u91cd\u5199\u5c5e\u6027\u503c\u4e0d\u4f1a\u62a5\u9519\uff0c\u4f46\u662f\u64cd\u4f5c\u5931\u8d25\uff0c\u800c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b 3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>configurable<\/em> \u53ef\u4ee5\u7981\u6b62\u4fee\u6539\u5c5e\u6027\u63cf\u8ff0\u7b26\uff0c\u5f53\u5176\u503c\u4e3a <em>false<\/em> \u65f6\uff0c<em>value\u3001writable\u3001enumerable<\/em> \u548c <em>configurable<\/em> \u7981\u6b62\u4fee\u6539\uff0c\u540c\u65f6\u7981\u6b62\u5220\u9664\u5c5e\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0b\u9762\u793a\u4f8b\u4e2d\uff0c\u5f53\u8bbe\u7f6e\u5c5e\u6027 <em>x<\/em> \u7981\u6b62\u4fee\u6539\u914d\u7f6e\u540e\uff0c\u4e0b\u9762\u64cd\u4f5c\u90fd\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u5176\u4e2d <em>obj.x=5;<\/em> \u82e5\u64cd\u4f5c\u5931\u8d25\uff0c\u5219\u540e\u9762 <em>4<\/em> \u4e2a\u64cd\u4f5c\u65b9\u6cd5\u90fd\u5c06\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = Object.defineProperty({}, 'x', {<br> &nbsp; &nbsp;configurable : false &nbsp;\/\/ \u7981\u6b62\u914d\u7f6e<br>});<br>obj.x = 5; &nbsp;\/\/\u8bd5\u56fe\u4fee\u6539\u5176\u503c<br>console.log(obj.x); &nbsp;\/\/\u4fee\u6539\u5931\u8d25\uff0c\u8fd4\u56deundefined<br>Object.defineProperty(obj, 'x', {value : 2}); &nbsp;\/\/\u629b\u51fa\u5f02\u5e38<br>Object.defineProperty(obj, 'x', {writable: true}); &nbsp;\/\/\u629b\u51fa\u5f02\u5e38<br>Object.defineProperty(obj, 'x', {enumerable: true}); &nbsp;\/\/\u629b\u51fa\u5f02\u5e38<br>Object.defineProperty(obj, 'x', {configurable: true}); &nbsp;\/\/\u629b\u51fa\u5f02\u5e38<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53 <em>configurable<\/em> \u4e3a <em>false<\/em> \u65f6\uff0c\u5982\u679c\u628a <em>writable=true<\/em> \u6539\u4e3a <em>false<\/em> \u662f\u5141\u8bb8\u7684\u3002\u53ea\u8981 <em>writable<\/em> \u6216 <em>configurable<\/em> \u6709\u4e00\u4e2a\u4e3a <em>true<\/em>\uff0c\u5219 <em>value<\/em> \u4e5f\u5141\u8bb8\u4fee\u6539\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><em>get<\/em> \u548c <em>set<\/em> \u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u4f7f\u7528\u70b9\u8bed\u6cd5\u6216\u4e2d\u62ec\u53f7\u8bed\u6cd5\u8bbf\u95ee\u5c5e\u6027\u7684 <em>value<\/em> \u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8bbf\u95ee\u5668\uff0c\u5305\u62ec <em>set<\/em> \u548c <em>get<\/em> \u4e24\u4e2a\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2d\uff0c<em>set( )<\/em> \u51fd\u6570\u53ef\u4ee5\u8bbe\u7f6e <em>value<\/em> \u5c5e\u6027\u503c\uff0c\u800c <em>get( )<\/em> \u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6 <em>value<\/em> \u5c5e\u6027\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u501f\u52a9\u8bbf\u95ee\u5668\uff0c\u53ef\u4ee5\u4e3a\u5c5e\u6027\u7684 <em>value<\/em> \u8bbe\u8ba1\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u7981\u7528\u90e8\u5206\u7279\u6027\u3001\u8bbe\u8ba1\u8bbf\u95ee\u6761\u4ef6\u3001\u5229\u7528\u5185\u90e8\u53d8\u91cf\u6216\u5c5e\u6027\u8fdb\u884c\u6570\u636e\u5904\u7406\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><br>\u793a\u4f8b 1<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u793a\u4f8b\u8bbe\u8ba1\u5bf9\u8c61 <em>obj<\/em> \u7684 <em>x<\/em> \u5c5e\u6027\u503c\u5fc5\u987b\u4e3a\u6570\u5b57\u3002\u4e3a\u5c5e\u6027 <em>x<\/em> \u5b9a\u4e49\u4e86 <em>get<\/em> \u548c <em>set<\/em> \u7279\u6027\uff0c<em>obj.x<\/em> \u53d6\u503c\u65f6\uff0c\u5c31\u4f1a\u8c03\u7528 <em>get<\/em>\uff1b\u8d4b\u503c\u65f6\uff0c\u5c31\u4f1a\u8c03\u7528 <em>set<\/em>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = Object.create(Object.prototype, {<br> &nbsp; &nbsp;_x : { &nbsp;\/\/\u6570\u636e\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp;value : 1, &nbsp;\/\/\u521d\u59cb\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp;writable : true<br> &nbsp;  },<br> &nbsp; &nbsp;x : { &nbsp;\/\/\u8bbf\u95ee\u5668\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp;get : function () { &nbsp;\/\/getter<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return this._x; &nbsp;\/\/\u8fd4\u56de_x\u5c5e\u6027\u503c<br> &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp; &nbsp;set : function (value) { &nbsp;\/\/setter<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (typeof value != \"number\"){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new Error('\u8bf7\u8f93\u5165\u6570\u5b57');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this._x = value; &nbsp;\/\/\u8d4b\u503c<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>});<br>console.log(obj.x); &nbsp;\/\/1<br>obj.x = \"2\"; &nbsp;\/\/\u629b\u51fa\u5f02\u5e38<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b 2<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>JavaScript<\/em> \u4e5f\u652f\u6301\u4e00\u79cd\u7b80\u5199\u65b9\u6cd5\u3002\u9488\u5bf9\u793a\u4f8b <em>1<\/em>\uff0c\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u53ef\u4ee5\u5feb\u901f\u5b9a\u4e49\u5c5e\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {<br> &nbsp; &nbsp;_x : 1, &nbsp;\/\/ \u5b9a\u4e49 _x \u5c5e\u6027<br> &nbsp; &nbsp;get x() { return this._x }, &nbsp;\/\/\u5b9a\u4e49 x \u5c5e\u6027\u7684 getter<br> &nbsp; &nbsp;set x(value) { &nbsp;\/\/\u5b9a\u4e49 x \u5c5e\u6027\u7684 setter<br> &nbsp; &nbsp; &nbsp; &nbsp;if (typeof value != \"number\"){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new Error('\u8bf7\u8f93\u5165\u6570\u5b57');<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;this._x = value; &nbsp;\/\/ \u8d4b\u503c<br> &nbsp;  }<br>};<br>console.log(obj.x); &nbsp;\/\/1<br>obj.x = 2;<br>console.log(obj.x); &nbsp;\/\/2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53d6\u503c\u51fd\u6570 <em>get( )<\/em> \u4e0d\u80fd\u63a5\u6536\u53c2\u6570\uff0c\u5b58\u503c\u51fd\u6570 <em>set( )<\/em> \u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u7528\u4e8e\u8bbe\u7f6e\u5c5e\u6027\u7684\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u64cd\u4f5c\u5c5e\u6027\u63cf\u8ff0\u7b26<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c5e\u6027\u63cf\u8ff0\u7b26\u662f\u4e00\u4e2a\u5185\u90e8\u5bf9\u8c61\uff0c\u65e0\u6cd5\u76f4\u63a5\u8bfb\u5199\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u51e0\u4e2a\u51fd\u6570\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>Object.getOwnPropertyDescriptor( )<\/em>\uff1a\u53ef\u4ee5\u8bfb\u51fa\u6307\u5b9a\u5bf9\u8c61\u79c1\u6709\u5c5e\u6027\u7684\u5c5e\u6027\u63cf\u8ff0\u7b26\u3002<\/li>\n\n\n\n<li><em>Object.defineProperty( )<\/em>\uff1a\u901a\u8fc7\u5b9a\u4e49\u5c5e\u6027\u63cf\u8ff0\u7b26\u6765\u5b9a\u4e49\u6216\u4fee\u6539\u4e00\u4e2a\u5c5e\u6027\uff0c\u7136\u540e\u8fd4\u56de\u4fee\u6539\u540e\u7684\u63cf\u8ff0\u7b26\u3002<\/li>\n\n\n\n<li><em>Object.defineProperties( )<\/em>\uff1a\u53ef\u4ee5\u540c\u65f6\u5b9a\u4e49\u591a\u4e2a\u5c5e\u6027\u63cf\u8ff0\u7b26\u3002<\/li>\n\n\n\n<li><em>Object.getOwnPropertyNames( )<\/em>\uff1a\u83b7\u53d6\u5bf9\u8c61\u7684\u6240\u6709\u79c1\u6709\u5c5e\u6027\u3002<\/li>\n\n\n\n<li><em>Object.keys( )<\/em>\uff1a\u83b7\u53d6\u5bf9\u8c61\u7684\u6240\u6709\u672c\u5730\u53ef\u679a\u4e3e\u7684\u5c5e\u6027\u3002<\/li>\n\n\n\n<li><em>propertyIsEnumerable( )<\/em>\uff1a\u5bf9\u8c61\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u76f4\u63a5\u8c03\u7528\uff0c\u5224\u65ad\u6307\u5b9a\u7684\u5c5e\u6027\u662f\u5426\u53ef\u679a\u4e3e\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b 1<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0b\u9762\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49 <em>obj<\/em> \u7684 <em>x<\/em> \u5c5e\u6027\u5141\u8bb8\u914d\u7f6e\u7279\u6027\uff0c\u7136\u540e\u4f7f\u7528 <em>Object.getOwnPropertyDescriptor( )<\/em> \u51fd\u6570\u83b7\u53d6\u5bf9\u8c61 <em>obj<\/em> \u7684 <em>x<\/em> \u5c5e\u6027\u7684\u5c5e\u6027\u63cf\u8ff0\u7b26\u3002\u4fee\u6539\u5c5e\u6027\u63cf\u8ff0\u7b26\u7684 <em>set<\/em> \u51fd\u6570\uff0c\u91cd\u8bbe\u68c0\u6d4b\u6761\u4ef6\uff0c\u5141\u8bb8\u975e\u6570\u503c\u578b\u6570\u5b57\u8d4b\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = Object.create(Object.prototype, {<br> &nbsp; &nbsp;_x: { &nbsp;\/\/\u6570\u636e\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp;value: 1, &nbsp;\/\/\u521d\u59cb\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp;writable: true<br> &nbsp;  },<br> &nbsp; &nbsp;x: { &nbsp;\/\/\u8bbf\u95ee\u5668\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp;configurable: true, &nbsp;\/\/\u5141\u8bb8\u4fee\u6539\u914d\u7f6e<br> &nbsp; &nbsp; &nbsp; &nbsp;get: function () { &nbsp;\/\/getter<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return this._x; &nbsp;\/\/\u8fd4\u56de_x\u5c5e\u6027\u503c<br> &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp; &nbsp;set: function (value) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (typeof value != \"number\") {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new Error('\u8bf7\u8f93\u5165\u6570\u5b57');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this._x = value; &nbsp;\/\/\u8d4b\u503c<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>});<br>var des = Object.getOwnPropertyDescriptor(obj, \"x\"); &nbsp;\/\/\u83b7\u53d6\u5c5e\u6027x\u7684\u5c5e\u6027\u63cf\u8ff0\u7b26<br>des.set = function (value) {<br> &nbsp; &nbsp;\/\/\u4fee\u6539\u5c5e\u6027x\u7684\u5c5e\u6027\u63cf\u8ff0\u7b26set\u51fd\u6570<br> &nbsp; &nbsp;\/\/\u5141\u8bb8\u975e\u6570\u503c\u578b\u7684\u6570\u5b57\uff0c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u8d4b\u503c<br> &nbsp; &nbsp;if (typeof value != \"number\" &amp;&amp; isNaN(value * 1)) {<br> &nbsp; &nbsp; &nbsp; &nbsp;throw new Error('\u8bf7\u8f93\u5165\u6570\u5b57');<br> &nbsp;  }<br> &nbsp; &nbsp;this._x = value;<br>}<br>obj = Object.defineProperty(obj, \"x\", des);<br>console.log(obj.x); &nbsp;\/\/1<br>obj.x = \"2\"; &nbsp;\/\/\u628a\u4e00\u4e2a\u7ed9\u6570\u503c\u578b\u6570\u5b57\u8d4b\u503c\u7ed9\u5c5e\u6027x<br>console.log(obj.x); &nbsp;\/\/2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b 2<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u793a\u4f8b\u5148\u5b9a\u4e49\u4e00\u4e2a\u6269\u5c55\u51fd\u6570\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u628a\u4e00\u4e2a\u5bf9\u8c61\u5305\u542b\u7684\u5c5e\u6027\u4ee5\u53ca\u4e30\u5bcc\u7684\u4fe1\u606f\u590d\u5236\u7ed9\u53e6\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3010\u5b9e\u73b0\u4ee3\u7801\u3011<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function extend (toObj, fromObj) { &nbsp;\/\/\u6269\u5c55\u5bf9\u8c61<br> &nbsp; &nbsp;for (var property in fromObj) { &nbsp;\/\/\u904d\u5386\u5bf9\u8c61\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp;if (!fromObj.hasOwnProperty(property)) continue; &nbsp;\/\/\u8fc7\u6ee4\u6389\u7ee7\u627f\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp;Object.defineProperty( &nbsp;\/\/\u590d\u5236\u5b8c\u6574\u7684\u5c5e\u6027\u4fe1\u606f<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;toObj, &nbsp;\/\/\u76ee\u6807\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;property, &nbsp;\/\/\u79c1\u6709\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Object.getOwnPropertyDescriptor(fromObj, property) &nbsp;\/\/\u83b7\u53d6\u5c5e\u6027\u63cf\u8ff0\u7b26<br> &nbsp; &nbsp; &nbsp;  );<br> &nbsp;  }<br> &nbsp; &nbsp;return toObj; &nbsp;\/\/\u8fd4\u56de\u76ee\u6807\u5bf9\u8c61<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3010\u5e94\u7528\u4ee3\u7801\u3011<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {}; &nbsp;\/\/\u65b0\u5efa\u5bf9\u8c61<br>obj.x = 1; &nbsp;\/\/\u5b9a\u4e49\u5bf9\u8c61\u5c5e\u6027<br>extend(obj, { get y() { return 2} }) &nbsp;\/\/\u5b9a\u4e49\u8bfb\u53d6\u5668\u5bf9\u8c61<br>console.log(obj.y); &nbsp;\/\/2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u63a7\u5236\u5bf9\u8c61\u72b6\u6001<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>JavaScript<\/em> \u63d0\u4f9b\u4e86 <em>3<\/em> \u79cd\u65b9\u6cd5\uff0c\u7528\u6765\u7cbe\u786e\u63a7\u5236\u4e00\u4e2a\u5bf9\u8c61\u7684\u8bfb\u5199\u72b6\u6001\uff0c\u9632\u6b62\u5bf9\u8c61\u88ab\u6539\u53d8\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>Object.preventExtensions<\/em>\uff1a\u963b\u6b62\u4e3a\u5bf9\u8c61\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\u3002<\/li>\n\n\n\n<li><em>Object.seal<\/em>\uff1a\u963b\u6b62\u4e3a\u5bf9\u8c61\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\uff0c\u540c\u65f6\u4e5f\u65e0\u6cd5\u5220\u9664\u65e7\u5c5e\u6027\u3002\u7b49\u4ef7\u4e8e\u5c5e\u6027\u63cf\u8ff0\u7b26\u7684 <em>configurable<\/em> \u5c5e\u6027\u8bbe\u4e3a <em>false<\/em>\u3002\u6ce8\u610f\uff0c\u8be5\u65b9\u6cd5\u4e0d\u5f71\u54cd\u4fee\u6539\u67d0\u4e2a\u5c5e\u6027\u7684\u503c\u3002<\/li>\n\n\n\n<li><em>Object.freeze<\/em>\uff1a\u963b\u6b62\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u6dfb\u52a0\u65b0\u5c5e\u6027\u3001\u5220\u9664\u65e7\u5c5e\u6027\u3001\u4fee\u6539\u5c5e\u6027\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u65f6\u63d0\u4f9b\u4e86 <em>3<\/em> \u4e2a\u5bf9\u5e94\u7684\u8f85\u52a9\u68c0\u67e5\u51fd\u6570\uff0c\u7b80\u5355\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>Object.isExtensible<\/em>\uff1a\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u5141\u8bb8\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\u3002<\/li>\n\n\n\n<li><em>Object.isSealed<\/em>\uff1a\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u4f7f\u7528\u4e86 <em>Object.seal<\/em> \u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><em>Object.isFrozen<\/em>\uff1a\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u4f7f\u7528\u4e86 <em>Object.freeze<\/em> \u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4ee3\u7801\u5206\u522b\u4f7f\u7528 <em>Object.preventExtensions\u3001Object.seal<\/em> \u548c <em>Object.freeze<\/em> \u51fd\u6570\u63a7\u5236\u5bf9\u8c61\u7684\u72b6\u6001\uff0c\u7136\u540e\u518d\u4f7f\u7528 <em>Object.isExtensible\u3001Object.isSealed<\/em> \u548c <em>Object.isFrozen<\/em> \u51fd\u6570\u68c0\u6d4b\u5bf9\u8c61\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj1 = {};<br>console.log(Object.isExtensible(obj1)); &nbsp;\/\/true<br>Object.preventExtensions(obj1);<br>console.log(Object.isExtensible(obj1)); &nbsp;\/\/false<br>var obj2 = {};<br>console.log(Object.isSealed(obj2)); &nbsp;\/\/true<br>Object.seal(obj2);<br>console.log(Object.isSealed(obj2)); &nbsp;\/\/false<br>var obj3 = {};<br>console.log(Object.isFrozen(obj3)); &nbsp;\/\/true<br>Object.freeze(obj3);<br>console.log(Object.isFrozen(obj3)); &nbsp;\/\/false<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5c5e\u6027\u63cf\u8ff0\u7b26\u8be6\u89e3 \u5728 JavaScript \u4e2d\uff0c\u5bf9\u8c61..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/uncategorized\/\" rel=\"category tag\">\u672a\u5206\u7c7b<\/a>","tag_info":"\u672a\u5206\u7c7b","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":3,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/209\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}