{"id":233347,"date":"2022-01-09T09:09:50","date_gmt":"2022-01-09T01:09:50","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=233347"},"modified":"2021-12-30T13:10:29","modified_gmt":"2021-12-30T05:10:29","slug":"js-tostring-object","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/js-tostring-object.html","title":{"rendered":"js\u4e2d\u7684toString\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u5206\u4eab\u4e86js\u4e2dtoString\u65b9\u6cd5\u76843\u4e2a\u4f5c\u7528\uff0c\u6587\u7ae0\u56f4\u7ed5js\u4e2dtoString\u65b9\u6cd5\u7684\u76f8\u5173\u8d44\u6599\u5c55\u5f00\u5168\u6587\u5185\u5bb9\uff0c\u9700\u8981\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001toString\u65b9\u6cd5\u7684\u4e09\u4e2a\u4f5c\u7528<\/strong><\/div>\n

1.\u8fd4\u56de\u4e00\u4e2a\u3010\u8868\u793a\u5bf9\u8c61\u3011\u7684\u3010\u5b57\u7b26\u4e32\u3011<\/p>\n

2.\u68c0\u6d4b\u5bf9\u8c61\u7684\u7c7b\u578b <\/p>\n

\r\nObject.prototype.toString.call(arr)===\"[object Array]\"<\/pre>\n

3.\u8fd4\u56de\u8be5\u6570\u5b57\u5bf9\u5e94\u8fdb\u5236\u7684\u5b57\u7b26\u4e32\u3002 <\/p>\n

\r\nconsole.log(10.toString(2)) \/\/10\u4e13\u4e3a\u4e3a2\u8fdb\u5236'1010'<\/pre>\n

\u5728js\u4e2d\u5305\u542b2\u8fdb\u5236\uff0c8\u8fdb\u5236\uff0c10\u8fdb\u5236\uff0c18\u8fdb\u5236\u3002<\/p>\n

\u4e8c\u3001\u8fd4\u56de\u4e00\u4e2a\u3010\u8868\u793a\u5bf9\u8c61\u3011\u7684\u3010\u5b57\u7b26\u4e32\u3011<\/strong><\/div>\n
Object.prototype.toString()<\/pre>\n

1.toString\u662f\u5c5e\u4e8eObject\u539f\u578b\u4e0a\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n

\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a toString()\u65b9\u6cd5\u3002 \u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\uff0ctoString()\u65b9\u6cd5\u88ab\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u7ee7\u627f\u3002\u5982\u679ctoString\u6ca1\u6709\u88ab\u5b9a\u4e49\u7684\u5bf9\u8c61\u8986\u76d6\u3002toString\u8fd4\u56de '[object type]' \u5176\u4e2dtype\u662f\u5bf9\u8c61\u7684\u7c7b\u578b,type\u7684\u503c\u53ef\u4ee5\u662fObject\uff0c<\/p>\n

\u4ee3\u7801\uff1a<\/p>\n

\r\nclass Person{\r\n  constructor(name,age){\r\n    this.name=name\r\n    this.age=age\r\n  }\r\n}\r\nlet zs=new Person('\u5f20\u4e09',18)\r\nconsole.log( zs.toString() ) \/\/ [object Object]\r\n<\/pre>\n

\u901a\u8fc7\u4e0a\u9762\u7684\u8f93\u51fa\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u5b9a\u3002<\/p>\n

\u8fd4\u56de\u6765\u7684\u786e\u5b9e\u662f\u8fd4\u56de\u4e00\u4e2a\u3010\u8868\u793a\u5bf9\u8c61\u3011\u7684\u3010\u5b57\u7b26\u4e32\u3011<\/p>\n

\u4e09\u3001\u81ea\u5b9a\u4e49\u7684toString()<\/strong><\/div>\n

\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5 \u53bb\u8986\u76d6\u9ed8\u8ba4\u7684toString\u65b9\u6cd5\u3002<\/p>\n

\u81ea\u5b9a\u4e49\u7684toString()\u65b9\u6cd5\u4e0d\u80fd\u591f\u4f20\u5165\u53c2\u6570\uff0c\u5e76\u4e14\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u5b9a\u4e49\u7684toString\u53ef\u4ee5\u8fd4\u56de\u6211\u4eec\u4efb\u4f55\u9700\u8981\u7684\u503c\uff0c\u5982\u679c\u4ed6\u80fd\u591f\u9644\u5e26\u4efb\u4f55\u6709\u5173\u5bf9\u8c61\u7684\u4fe1\u606f\uff0c\u4ed6\u5c06\u53d8\u6210\u975e\u5e38\u6709\u7528<\/p>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nclass Person{\r\n  constructor(name,age){\r\n    this.name=name\r\n    this.age=age\r\n  }\r\n  \/\/ \u91cd\u5199Object.prototype.toString()\r\n  toString(){\r\n    return `Person{name=${this.name},age=${this.age}}`\r\n  }\r\n}\r\nlet zs=new Person('\u5f20\u4e09',18)\r\nconsole.log( zs.toString() ) \/\/Person{name=\u5f20\u4e09,age=18}\r\n<\/pre>\n

JavaScript\u7684\u8bb8\u591a\u5185\u7f6e\u5bf9\u8c61\u90fd\u91cd\u5199\u4e86\u8be5\u51fd\u6570\uff0c\u4ee5\u5b9e\u73b0\u66f4\u9002\u5408\u81ea\u8eab\u7684\u529f\u80fd\u9700\u8981.<\/p>\n

1. Array\u7684\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5b83\u4eec\u4f9d\u6b21\u8fde\u63a5\u8d77\u6765\uff0c\u4e24\u4e2a\u5143\u7d20\u4e4b\u95f4\u7528\u82f1\u6587\u9017\u53f7\u4f5c\u4e3a<\/p>\n

2.Boolean \u5982\u679c\u5e03\u5c14\u503c\u662ftrue\uff0c\u5219\u8fd4\u56de\"true\"\u3002\u5426\u5219\u8fd4\u56de\"false\"\u3002<\/p>\n

3.Date \u8fd4\u56de\u65e5\u671f\u7684\u6587\u672c\u8868\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

1.\u8fd4\u56de\u4e00\u4e2a\u3010\u8868\u793a\u5bf9\u8c61\u3011\u7684\u3010\u5b57\u7b26\u4e32\u3011 2.\u68c0\u6d4b\u5bf9\u8c61\u7684\u7c7b\u578b Object.prototype.toString. […]<\/p>\n","protected":false},"author":362,"featured_media":156047,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-233347","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=233347"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233347\/revisions"}],"predecessor-version":[{"id":233350,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233347\/revisions\/233350"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/156047"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=233347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=233347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=233347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}