{"id":2486,"date":"2024-11-10T17:45:39","date_gmt":"2024-11-10T09:45:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2486\/"},"modified":"2024-11-10T17:45:39","modified_gmt":"2024-11-10T09:45:39","slug":"javascript-%e5%af%b9%e8%b1%a1%e6%96%b9%e6%b3%95%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2486\/","title":{"rendered":"JavaScript \u5bf9\u8c61\u65b9\u6cd5\u793a\u4f8b"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173077525212143.jpg\" class=\"aligncenter\" title=\"JavaScript \u5bf9\u8c61\u65b9\u6cd5\u793a\u4f8b\u63d2\u56fe\" alt=\"JavaScript \u5bf9\u8c61\u65b9\u6cd5\u793a\u4f8b\u63d2\u56fe\" \/><\/p>\n<h2> javascript \u5bf9\u8c61\u65b9\u6cd5\u793a\u4f8b\u3002 <\/h2>\n<ul>\n<li> <strong>object.keys(obj):<\/strong> \u8fd4\u56de\u5bf9\u8c61\u81ea\u5df1\u7684\u53ef\u679a\u4e3e\u5c5e\u6027\u540d\u79f0\uff08\u952e\uff09\u7684\u6570\u7ec4\u3002 <\/li>\n<\/ul>\n<pre>const obj = { a: 1, b: 2, c: 3 };\nconsole.log(object.keys(obj));\n\/\/ output: ['a', 'b', 'c']\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.values(obj):<\/strong> \u8fd4\u56de\u5bf9\u8c61\u81ea\u5df1\u7684\u53ef\u679a\u4e3e\u5c5e\u6027\u503c\u7684\u6570\u7ec4\u3002 <\/li>\n<\/ul>\n<pre>const obj = { a: 1, b: 2, c: 3 };\nconsole.log(object.values(obj));\n\/\/ output: [1, 2, 3]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.entries(obj):<\/strong> \u8fd4\u56de\u5bf9\u8c61\u81ea\u5df1\u7684\u53ef\u679a\u4e3e\u5b57\u7b26\u4e32\u952e\u63a7\u5c5e\u6027 [key, value] \u5bf9\u7684\u6570\u7ec4\u3002 <\/li>\n<\/ul>\n<pre>const obj = { a: 1, b: 2, c: 3 };\nconsole.log(object.entries(obj));\n\/\/ output: [['a', 1], ['b', 2], ['c', 3]]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.issealed(obj):<\/strong> \u5982\u679c\u5bf9\u8c61\u662f\u5bc6\u5c01\u7684\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002 <\/li>\n<\/ul>\n<pre>const obj = object.seal({ a: 1 });\nconsole.log(object.issealed(obj));\n\/\/ output: true\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.assign(target, source):<\/strong> \u5c06\u6240\u6709\u53ef\u679a\u4e3e\u5c5e\u6027\u7684\u503c\u4ece\u4e00\u4e2a\u6216\u591a\u4e2a\u6e90\u5bf9\u8c61\u590d\u5236\u5230\u76ee\u6807\u5bf9\u8c61\u3002\u5b83\u8fd4\u56de\u76ee\u6807\u5bf9\u8c61\u3002 <\/li>\n<\/ul>\n<pre>const target = { a: 1 };\nconst source = { b: 2, c: 3 };\nconst result = object.assign(target, source);\nconsole.log(result);\n\/\/ output: { a: 1, b: 2, c: 3 }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.freeze(obj):<\/strong> \u51bb\u7ed3\u5bf9\u8c61\uff0c\u9632\u6b62\u6dfb\u52a0\u65b0\u5c5e\u6027\u6216\u5220\u9664\u6216\u91cd\u65b0\u914d\u7f6e\u73b0\u6709\u5c5e\u6027\u3002 <\/li>\n<\/ul>\n<pre>const obj = { name: 'khabib' };\nobject.freeze(obj);\nobj.name = 'bob'; \/\/ this won't change the value\nconsole.log(obj.name); \/\/ output: 'khabib'\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.seal(obj):<\/strong> \u5bc6\u5c01\u5bf9\u8c61\uff0c\u9632\u6b62\u6dfb\u52a0\u65b0\u5c5e\u6027\uff0c\u4f46\u5141\u8bb8\u4fee\u6539\u73b0\u6709\u5c5e\u6027\u3002 <\/li>\n<\/ul>\n<pre>const obj = { name: 'alice' };\nobject.seal(obj);\nobj.name = 'bob'; \/\/ this will update the value\nobj.age = 25; \/\/ this won't add a new property\nconsole.log(obj); \/\/ output: { name: 'bob' }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.create(proto):<\/strong> \u4f7f\u7528\u6307\u5b9a\u7684\u539f\u578b\u5bf9\u8c61\u548c\u5c5e\u6027\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\u3002 <\/li>\n<\/ul>\n<pre>const person = {greet() {console.log('hello!');}};\nconst student = object.create(person);\nstudent.greet();\n\/\/ output: 'hello!'\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.defineproperty(obj, prop, detector):<\/strong> \u76f4\u63a5\u5728\u5bf9\u8c61\u4e0a\u5b9a\u4e49\u65b0\u5c5e\u6027\u6216\u4fee\u6539\u73b0\u6709\u5c5e\u6027\u3002 <\/li>\n<\/ul>\n<pre>const obj = {};\nobject.defineproperty(obj, 'name', {\nvalue: 'alice',\nwritable: false });\nconsole.log(obj.name); \/\/ 'alice'\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.defineproperties(obj, props):<\/strong> \u5b9a\u4e49\u591a\u4e2a\u65b0\u5c5e\u6027\u6216\u4fee\u6539\u5bf9\u8c61\u7684\u73b0\u6709\u5c5e\u6027\u3002 <\/li>\n<\/ul>\n<pre>const obj = {};\nobject.defineproperties(obj, {\nname: { value: 'cormier', writable: false },\nage: { value: 30, writable: true } });\nconsole.log(obj.name); \/\/ 'cormier'\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.isextensible(obj):<\/strong> \u786e\u5b9a\u5bf9\u8c61\u662f\u5426\u53ef\u6269\u5c55\uff08\u5373\u662f\u5426\u53ef\u4ee5\u6dfb\u52a0\u65b0\u5c5e\u6027\uff09\u3002 <\/li>\n<\/ul>\n<pre>const obj = {};\nconsole.log(object.isextensible(obj)); \/\/ true\nobject.preventextensions(obj);\nconsole.log(object.isextensible(obj)); \/\/ false\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.isfrozen(obj):<\/strong> \u786e\u5b9a\u5bf9\u8c61\u662f\u5426\u88ab\u51bb\u7ed3\uff08\u5373\u4e0d\u53ef\u6269\u5c55\u4e14\u6240\u6709\u5c5e\u6027\u4e0d\u53ef\u5199\uff09\u3002 <\/li>\n<\/ul>\n<pre>const obj = object.freeze({ name: 'gregor' });\nconsole.log(object.isfrozen(obj));\n\/\/ output: true\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.hasown(obj, prop):<\/strong> \u5982\u679c\u6307\u5b9a\u5bf9\u8c61\u5c06\u6307\u5b9a\u5c5e\u6027\u4f5c\u4e3a\u5176\u81ea\u5df1\u7684\u5c5e\u6027\uff0c\u5219\u8fd4\u56de true\uff0c\u5373\u4f7f\u8be5\u5c5e\u6027\u7684\u503c\u672a\u5b9a\u4e49\u3002 <\/li>\n<\/ul>\n<pre>const obj = { name: 'alice' };\nconsole.log(object.hasown(obj, 'name')); \/\/ true\nconsole.log(object.hasown(obj, 'age')); \/\/ false\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.hasownproperty(prop):<\/strong> \u786e\u5b9a\u5bf9\u8c61\u662f\u5426\u5305\u542b\u6307\u5b9a\u5c5e\u6027\u4f5c\u4e3a\u8be5\u5bf9\u8c61\u7684\u76f4\u63a5\u5c5e\u6027\uff0c\u800c\u4e0d\u662f\u901a\u8fc7\u539f\u578b\u94fe\u7ee7\u627f\u3002 <\/li>\n<\/ul>\n<pre>const obj = { name: 'alice' };\nconsole.log(obj.hasownproperty('name')); \/\/ true\nconsole.log(obj.hasownproperty('age')); \/\/ false\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.preventextensions(obj):<\/strong> \u9632\u6b62\u5c06\u65b0\u5c5e\u6027\u6dfb\u52a0\u5230\u5bf9\u8c61\u4e2d\u3002 <\/li>\n<\/ul>\n<pre>const obj = {};\nobject.preventextensions(obj);\nobj.name = 'khabib'; \/\/ won't be added\nconsole.log(obj); \/\/ {}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.setprototypeof(obj, proto):<\/strong> \u8bbe\u7f6e\u6307\u5b9a\u5bf9\u8c61\u7684\u539f\u578b\uff08\u5185\u90e8 [[prototype]] \u5c5e\u6027\uff09\u3002 <\/li>\n<\/ul>\n<pre>const proto = { greet() {console.log('hello!');}};\nconst obj = {};\nobject.setprototypeof(obj, proto);\nobj.greet(); \/\/ 'hello!'\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.fromentries(iterable):<\/strong> \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5bf9\u8c61\u3002 <\/li>\n<\/ul>\n<pre>const entries = [['name', 'rock'], ['age', 35]];\nconst obj = object.fromentries(entries);\nconsole.log(obj); \/\/ { name: 'rock', age: 35 }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.getprototypeof(obj)<\/strong>\uff1a\u8fd4\u56de\u6307\u5b9a\u5bf9\u8c61\u7684\u539f\u578b\uff08\u5185\u90e8 [[prototype]] \u5c5e\u6027\uff09\u3002 <\/li>\n<\/ul>\n<pre>const obj = {};\nconst proto = object.getprototypeof(obj);\nconsole.log(proto === object.prototype); \/\/ true\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.getownpropertysymbols(obj):<\/strong> \u8fd4\u56de\u5728\u5bf9\u8c61\u4e0a\u627e\u5230\u7684\u6240\u6709\u7b26\u53f7\u5c5e\u6027\u7684\u6570\u7ec4\u3002 <\/li>\n<\/ul>\n<pre>const symbol = symbol('id');\nconst obj = { [symbol]: 123 };\nconst symbols = object.getownpropertysymbols(obj);\nconsole.log(symbols); \/\/ [symbol(id)]\nconsole.log(obj[symbols[0]]); \/\/ 123\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.getownpropertydescriptor(obj, prop):<\/strong> \u8fd4\u56de\u7ed9\u5b9a\u5bf9\u8c61\u7684\u7279\u5b9a\u5c5e\u6027\u7684\u5c5e\u6027\u63cf\u8ff0\u7b26\u3002 <\/li>\n<\/ul>\n<pre>const obj = { name: 'alice', age: 26 };\nconst descriptor = object.getownpropertydescriptor(obj, 'name');\nconsole.log(descriptor);\n\/\/ output: { configurable: true, enumerable: true, value: \"alice\", writable: true }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.getownpropertynames(obj):<\/strong> \u8fd4\u56de\u5728\u5bf9\u8c61\u4e0a\u627e\u5230\u7684\u6240\u6709\u5c5e\u6027\uff08\u5305\u62ec\u4e0d\u53ef\u679a\u4e3e\u5c5e\u6027\uff09\u7684\u6570\u7ec4\u3002 <\/li>\n<\/ul>\n<p> 9688635538\u200b\u200b35 <\/p>\n<ul>\n<li> <strong>object.is(value1, value2):<\/strong> \u6bd4\u8f83\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u540c\u3002 <\/li>\n<\/ul>\n<pre>console.log(object.is('foo', 'foo')); \/\/ true\nconsole.log(object.is({}, {})); \/\/ false\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>object.getownpropertydescriptors(obj):<\/strong> \u8fd4\u56de\u5bf9\u8c61\u7684\u6240\u6709\u81ea\u6709\u5c5e\u6027\u63cf\u8ff0\u7b26\u3002 <\/li>\n<\/ul>\n<pre>const obj = { name: 'Khabib', age: 28 };\nconst descriptors = Object.getOwnPropertyDescriptors(obj);\nconsole.log(descriptors);\n\/\/ Output: {\nage: {\nconfigurable: true,\nenumerable: true,\nvalue: 28,\nwritable: true },\nname: {\n  configurable: true,\n  enumerable: true,\n  value: \"Khabib\",\n  writable: true\n }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u5bf9\u8c61\u65b9\u6cd5\u793a\u4f8b\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>javascript \u5bf9\u8c61\u65b9\u6cd5\u793a\u4f8b\u3002 object.keys(obj): \u8fd4\u56de\u5bf9\u8c61\u81ea\u5df1\u7684\u53ef\u679a\u4e3e\u5c5e\u6027\u540d\u79f0\uff08\u952e\uff09\u7684\u6570\u7ec4\u3002 const obj = { a: 1, b: 2, c: 3 }; console.log(object.keys(obj)); \/\/ output: [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;] \u767b\u5f55\u540e\u590d\u5236 object.values(obj): \u8fd4\u56de\u5bf9\u8c61\u81ea\u5df1\u7684\u53ef\u679a\u4e3e\u5c5e\u6027\u503c\u7684\u6570\u7ec4\u3002 const obj = { a: 1, b: 2, c: 3 }; console.log(object.values(obj)); \/\/ output: [1, 2, 3] \u767b\u5f55\u540e\u590d\u5236 object.entries(obj): \u8fd4\u56de\u5bf9\u8c61\u81ea\u5df1\u7684\u53ef\u679a\u4e3e\u5b57\u7b26\u4e32\u952e\u63a7\u5c5e\u6027 [key, value] \u5bf9\u7684\u6570\u7ec4\u3002 const obj = { a: 1, b: 2, [&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-2486","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2486","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=2486"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2486\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}