{"id":32622,"date":"2024-11-25T08:30:00","date_gmt":"2024-11-25T00:30:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32622\/"},"modified":"2024-11-25T08:30:00","modified_gmt":"2024-11-25T00:30:00","slug":"%e9%87%8d%e6%96%b0%e8%a1%a8%e8%be%be%e7%9a%84%e6%a0%87%e9%a2%98%e4%b8%ba%ef%bc%9athe-concept-of-object","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32622\/","title":{"rendered":"\u91cd\u65b0\u8868\u8fbe\u7684\u6807\u9898\u4e3a\uff1aThe Concept of Object()"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169375615196390.jpg\" class=\"aligncenter\" title=\"\u91cd\u65b0\u8868\u8fbe\u7684\u6807\u9898\u4e3a\uff1aThe Concept of Object()\u63d2\u56fe\" alt=\"\u91cd\u65b0\u8868\u8fbe\u7684\u6807\u9898\u4e3a\uff1aThe Concept of Object()\u63d2\u56fe\" \/><\/p>\n<p>\u4f7f\u7528\u5185\u7f6e\u7684 Object() \u6784\u9020\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u521b\u5efa\u901a\u7528\u7684\u7a7a\u5bf9\u8c61\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5982\u679c\u4f60\u8fd8\u8bb0\u5f97\u7b2c\u4e00\u7ae0\u7684\u5f00\u5934\uff0c\u8fd9\u6b63\u662f\u6211\u4eec\u901a\u8fc7\u521b\u5efa cody \u5bf9\u8c61\u6240\u505a\u7684\u4e8b\u60c5\u3002\u8ba9\u6211\u4eec\u91cd\u65b0\u521b\u5efa cody \u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p>\u793a\u4f8b\uff1asample69.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar cody = new Object(); \/\/ Create an empty object with no properties.\n\n\tfor (key in cody) { \/\/ Confirm that cody is an empty generic object.\n\t\tif (cody.hasOwnProperty(key)) {\n\t\t\tconsole.log(key); \/\/ Should not see any logs, because cody itself has no properties.\n\t\t}\n\t}\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u6240\u505a\u7684\u5c31\u662f\u4f7f\u7528 Object() \u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a cody \u7684\u901a\u7528\u5bf9\u8c61\u3002\u60a8\u53ef\u4ee5\u5c06 Object() \u6784\u9020\u51fd\u6570\u89c6\u4e3a\u4e00\u4e2a\u5343\u7bc7\u4e00\u5f8b\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u521b\u5efa\u6ca1\u6709\u9884\u5b9a\u4e49\u5c5e\u6027\u6216\u65b9\u6cd5\u7684\u7a7a\u5bf9\u8c61\uff08\u5f53\u7136\uff0c\u4ece\u539f\u578b\u94fe\u7ee7\u627f\u7684\u5bf9\u8c61\u9664\u5916\uff09\u3002 <\/p>\n<p>\u5982\u679c\u4e0d\u662f\u5f88\u660e\u663e\uff0cObject() \u6784\u9020\u51fd\u6570\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6784\u9020\u51fd\u6570\u57fa\u4e8e\u4ece Function \u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u8fd9\u53ef\u80fd\u4f1a\u4ee4\u4eba\u56f0\u60d1\u3002\u8bf7\u8bb0\u4f4f\uff0c\u4e0e Array \u6784\u9020\u51fd\u6570\u4e00\u6837\uff0cObject \u6784\u9020\u51fd\u6570\u53ea\u662f\u5410\u51fa\u7a7a\u767d\u5bf9\u8c61\u3002\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u60a8\u559c\u6b22\u7684\u6240\u6709\u7a7a\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u521b\u5efa\u50cf cody \u8fd9\u6837\u7684\u7a7a\u5bf9\u8c61\u4e0e\u4f7f\u7528\u9884\u5b9a\u4e49\u5c5e\u6027\u521b\u5efa\u81ea\u5df1\u7684\u6784\u9020\u51fd\u6570\u6709\u5f88\u5927\u4e0d\u540c\u3002\u786e\u4fdd\u60a8\u4e86\u89e3 cody \u53ea\u662f\u4e00\u4e2a\u57fa\u4e8e Object() \u6784\u9020\u51fd\u6570\u7684\u7a7a\u5bf9\u8c61\u3002\u8981\u771f\u6b63\u5229\u7528 JavaScript \u7684\u529b\u91cf\uff0c\u60a8\u4e0d\u4ec5\u9700\u8981\u5b66\u4e60\u5982\u4f55\u4ece Object() \u521b\u5efa\u7a7a\u5bf9\u8c61\u5bb9\u5668\uff0c\u8fd8\u9700\u8981\u5b66\u4e60\u5982\u4f55\u6784\u5efa\u60a8\u81ea\u5df1\u7684\u5bf9\u8c61\u201c\u7c7b\u201d(Person())\uff0c\u4f8b\u5982Object() \u6784\u9020\u51fd\u6570\u672c\u8eab\u3002<\/p>\n<hr>\n<h2> Object() \u53c2\u6570<\/h2>\n<p>Object() \u6784\u9020\u51fd\u6570\u91c7\u7528\u4e00\u4e2a\u53ef\u9009\u53c2\u6570\u3002\u8be5\u53c2\u6570\u662f\u60a8\u60f3\u8981\u521b\u5efa\u7684\u503c\u3002\u5982\u679c\u60a8\u672a\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\uff0c\u5219\u5c06\u5047\u5b9a null \u6216 undefined \u503c\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample70.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ Create an empty object with no properties.\n\tvar cody1 = new Object();\n\tvar cody2 = new Object(undefined);\n\tvar cody3 = new Object(null);\n\n\tconsole.log(typeof cody1, typeof cody2, typeof cody3); \/\/ Logs 'object object object'.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u5c06 null \u6216 undefined \u4e4b\u5916\u7684\u503c\u4f20\u9012\u7ed9 Object \u6784\u9020\u51fd\u6570\uff0c\u5219\u4f20\u9012\u7684\u503c\u5c06\u88ab\u521b\u5efa\u4e3a\u5bf9\u8c61\u3002\u56e0\u6b64\u7406\u8bba\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Object() \u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u4efb\u4f55\u5176\u4ed6\u5177\u6709\u6784\u9020\u51fd\u6570\u7684\u672c\u673a\u5bf9\u8c61\u3002\u5728\u4e0b\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u5c31\u662f\u8fd9\u4e48\u505a\u7684\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample71.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/* Use the Object() constructor to create string, number, array, function, Boolean, and regex objects. *\/\n\n\t\/\/ The following logs confirm object creation.\n\tconsole.log(new Object('foo'));\n\tconsole.log(new Object(1));\n\tconsole.log(new Object([]));\n\tconsole.log(new Object(function () { }));\n\tconsole.log(new Object(true));\n\tconsole.log(new Object(\/t[a-z]+\/));\n\n\t\/* Creating string, number, array, function, Boolean, and regex object instances via the Object() constructor is really never done. I am just demonstrating that it can be done. *\/\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<hr>\n<h2> Object() \u5c5e\u6027\u548c\u65b9\u6cd5<\/h2>\n<p>Object() \u5bf9\u8c61\u5177\u6709\u4ee5\u4e0b\u5c5e\u6027\uff08\u4e0d\u5305\u62ec\u7ee7\u627f\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff09\uff1a<\/p>\n<p>\u5c5e\u6027\uff08Object.prototype;\uff09\uff1a<\/p>\n<ul>\n<li>\u539f\u578b<\/li>\n<\/ul>\n<hr>\n<h2> Object() \u5b9e\u4f8b\u5c5e\u6027\u548c\u65b9\u6cd5<\/h2>\n<p>Object()\u5bf9\u8c61\u5b9e\u4f8b\u5177\u6709\u4ee5\u4e0b\u5c5e\u6027\u548c\u65b9\u6cd5\uff08\u4e0d\u5305\u62ec\u7ee7\u627f\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff09\uff1a<\/p>\n<p>\u5b9e\u4f8b\u5c5e\u6027 (var myObject = {}; myObject.constructor;):<\/p>\n<ul>\n<li>\u6784\u9020\u51fd\u6570<\/li>\n<\/ul>\n<p>\u5b9e\u4f8b\u65b9\u6cd5 (var myObject = {}; myObject.toString();): <\/p>\n<ul>\n<li>hasOwnProperty()<\/li>\n<li>isPrototypeOf()<\/li>\n<li>propertyIsEnumerable()<\/li>\n<li>toLocaleString()<\/li>\n<li>toString()<\/li>\n<li>valueOf()<\/li>\n<\/ul>\n<p>\u539f\u578b\u94fe\u4ee5Object.prototype\u7ed3\u5c3e\uff0c\u56e0\u6b64Object()\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u90fd\u4f1a\u88ab\u6240\u6709JavaScript\u5bf9\u8c61\u7ee7\u627f\u3002<\/p>\n<hr>\n<h2>\u4f7f\u7528\u201c\u5bf9\u8c61\u6587\u5b57\u201d\u521b\u5efa Object() \u5bf9\u8c61<\/h2>\n<p>\u521b\u5efa\u201c\u5bf9\u8c61\u6587\u5b57\u201d\u9700\u8981\u4f7f\u7528\u5927\u62ec\u53f7\u5b9e\u4f8b\u5316\u5e26\u6709\u6216\u4e0d\u5e26\u6709\u5c5e\u6027\u7684\u5bf9\u8c61\uff08var cody = {};\uff09\u3002\u8fd8\u8bb0\u5f97\u5728\u7b2c\u4e00\u7ae0\u5f00\u59cb\u65f6\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u6b21\u6027 cody \u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u70b9\u8868\u793a\u6cd5\u8d4b\u4e88 cody \u5bf9\u8c61\u5c5e\u6027\u5417\uff1f\u8ba9\u6211\u4eec\u518d\u505a\u4e00\u6b21\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample72.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar cody = new Object();\n\tcody.living = true;\n\tcody.age = 33;\n\tcody.gender = 'male';\n\tcody.getGender = function () { return cody.gender; };\n\n\tconsole.log(cody); \/\/ Logs cody object and properties.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u4ee3\u7801\u4e2d\u521b\u5efa cody \u5bf9\u8c61\u53ca\u5176\u5c5e\u6027\u9700\u8981\u4e94\u4e2a\u8bed\u53e5\u3002\u4f7f\u7528\u5bf9\u8c61\u6587\u5b57\u8868\u793a\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u6761\u8bed\u53e5\u4e2d\u8868\u8fbe\u76f8\u540c\u7684 cody \u5bf9\u8c61\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample73.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar cody = {\n\t\tliving: true,\n\t\tage: 23,\n\t\tgender: 'male',\n\t\tgetGender: function () { return cody.gender; }\n\t};\n\t\/\/ Notice the last property has no comma after it.\n\n\tconsole.log(cody); \/\/ Logs the cody object and its properties.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528\u6587\u5b57\u8868\u793a\u6cd5\u4f7f\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u66f4\u5c11\u7684\u4ee3\u7801\u521b\u5efa\u5bf9\u8c61\uff08\u5305\u62ec\u5b9a\u4e49\u7684\u5c5e\u6027\uff09\u5e76\u76f4\u89c2\u5730\u5c01\u88c5\u76f8\u5173\u6570\u636e\u3002\u8bf7\u6ce8\u610f\u5728\u5355\u4e2a\u8bed\u53e5\u4e2d\u4f7f\u7528 : \u548c , \u8fd0\u7b97\u7b26\u3002\u7531\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u8fd9\u5b9e\u9645\u4e0a\u662f\u5728 JavaScript \u4e2d\u521b\u5efa\u5bf9\u8c61\u7684\u9996\u9009\u8bed\u6cd5\u3002<\/p>\n<p>\u60a8\u5e94\u8be5\u77e5\u9053\u5c5e\u6027\u540d\u79f0\u4e5f\u53ef\u4ee5\u6307\u5b9a\u4e3a\u5b57\u7b26\u4e32\uff1a<\/p>\n<p>\u793a\u4f8b\uff1asample74.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar cody = {\n\t\t'living': true,\n\t\t'age': 23,\n\t\t'gender': 'male',\n\t\t'getGender': function () { return cody.gender; }\n\t};\n\n\tconsole.log(cody); \/\/ Logs the cody object and its properties.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6ca1\u6709\u5fc5\u8981\u5c06\u5c5e\u6027\u6307\u5b9a\u4e3a\u5b57\u7b26\u4e32\uff0c\u9664\u975e\u5c5e\u6027\u540d\u79f0\uff1a<\/p>\n<ul>\n<li>\u662f\u4fdd\u7559\u5173\u952e\u5b57\u4e4b\u4e00\uff08class\uff09\u3002<\/li>\n<li>\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\uff08\u6570\u5b57\u3001\u5b57\u6bcd\u3001\u7f8e\u5143\u7b26\u53f7 ($) \u6216\u4e0b\u5212\u7ebf (_) \u5b57\u7b26\u4ee5\u5916\u7684\u4efb\u4f55\u5b57\u7b26\uff09\u3002<\/li>\n<li>\u4ee5\u6570\u5b57\u5f00\u5934\u3002<\/li>\n<\/ul>\n<p>\u5c0f\u5fc3\uff01\u5bf9\u8c61\u7684\u6700\u540e\u4e00\u4e2a\u5c5e\u6027\u4e0d\u5e94\u6709\u5c3e\u968f\u9017\u53f7\u3002\u8fd9\u4f1a\u5728\u67d0\u4e9b JavaScript \u73af\u5883\u4e2d\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<hr>\n<h2>\u6240\u6709\u5bf9\u8c61\u7ee7\u627f\u81eaObject.prototype <\/h2>\n<p>JavaScript\u4e2d\u7684Object()\u6784\u9020\u51fd\u6570\u6bd4\u8f83\u7279\u6b8a\uff0c\u56e0\u4e3a\u5b83\u7684prototype\u5c5e\u6027\u662f\u539f\u578b\u94fe\u4e2d\u7684\u6700\u540e\u4e00\u7ad9\u3002<\/p>\n<p>\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4f7f\u7528 foo \u5c5e\u6027\u6269\u5145 Object.prototype\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e76\u5c1d\u8bd5\u8bbf\u95ee foo \u5c5e\u6027\uff0c\u5c31\u597d\u50cf\u5b83\u662f\u5b57\u7b26\u4e32\u5b9e\u4f8b\u7684\u5c5e\u6027\u4e00\u6837\u3002\u7531\u4e8e myString \u5b9e\u4f8b\u6ca1\u6709 foo \u5c5e\u6027\uff0c\u56e0\u6b64\u539f\u578b\u94fe\u542f\u52a8\u5e76\u5728 String.prototype \u4e2d\u67e5\u627e\u503c\u3002\u5b83\u4e0d\u5728\u90a3\u91cc\uff0c\u6240\u4ee5\u4e0b\u4e00\u4e2a\u8981\u67e5\u627e\u7684\u4f4d\u7f6e\u662f Object.prototype\uff0c\u8fd9\u662f JavaScript \u67e5\u627e\u5bf9\u8c61\u503c\u7684\u6700\u7ec8\u4f4d\u7f6e\u3002\u627e\u5230\u4e86 foo \u503c\uff0c\u56e0\u4e3a\u6211\u6dfb\u52a0\u4e86\u5b83\uff0c\u56e0\u6b64\u5b83\u8fd4\u56de foo \u7684\u503c\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample75.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tObject.prototype.foo = 'foo';\n\n\tvar myString = 'bar';\n\n\n\t\/\/ Logs 'foo', being found at Object.prototype.foo via the prototype chain.\n\tconsole.log(myString.foo);\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<hr>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u5c0f\u5fc3\uff01\u6dfb\u52a0\u5230 Object.prototype \u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u5c06\u663e\u793a\u5728 for in \u5faa\u73af\u548c\u539f\u578b\u94fe\u4e2d\u3002\u56e0\u6b64\uff0c\u636e\u8bf4\u7981\u6b62\u66f4\u6539 Object.prototype \u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u91cd\u65b0\u8868\u8fbe\u7684\u6807\u9898\u4e3a\uff1aThe Concept of Object()\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>\u4f7f\u7528\u5185\u7f6e\u7684 Object() \u6784\u9020\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u521b\u5efa\u901a\u7528\u7684\u7a7a\u5bf9\u8c61\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5982\u679c\u4f60\u8fd8\u8bb0\u5f97\u7b2c\u4e00\u7ae0\u7684\u5f00\u5934\uff0c\u8fd9\u6b63\u662f\u6211\u4eec\u901a\u8fc7\u521b\u5efa cody \u5bf9\u8c61\u6240\u505a\u7684\u4e8b\u60c5\u3002\u8ba9\u6211\u4eec\u91cd\u65b0\u521b\u5efa cody \u5bf9\u8c61\u3002 \u793a\u4f8b\uff1asample69.html &lt;script&gt; var cody = new Object(); \/\/ Create an empty object with no properties. for (key in cody) { \/\/ Confirm that cody is an empty generic object. if (cody.hasOwnProperty(key)) { console.log(key); \/\/ Should not see any logs, because cody itself has no properties. } } &lt;\/script&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32622","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32622","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=32622"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32622\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}