{"id":44,"date":"2007-07-25T17:10:17","date_gmt":"2007-07-25T21:10:17","guid":{"rendered":"http:\/\/prglab.com\/blog\/js-tutorial\/js-createobjs"},"modified":"2007-08-25T14:46:59","modified_gmt":"2007-08-25T18:46:59","slug":"js-createobjs","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-createobjs","title":{"rendered":"\u5728JavaScript\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u7684\u5bf9\u8c61"},"content":{"rendered":"<p>\u4f7f\u7528\u5bf9\u8c61\u4fbf\u4e8e\u7ec4\u7ec7\u4fe1\u606f\u3002\u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u5982\u4f55\u5728JavaScript\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u7684\u5bf9\u8c61\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<h2>JavaScript \u5bf9\u8c61<\/h2>\n<p>\u5728\u524d\u9762\u51e0\u7ae0\u4e2d\u6211\u4eec\u5b66\u5230JavaScript\u4e2d\u6709\u4e9b\u5185\u7f6e\u7684\u5bf9\u8c61\uff0c\u6bd4\u5982String, Date, Array\u7b49\u7b49\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u5bf9\u8c61\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6570\u636e\uff0c\u542b\u6709\u5c5e\u6027\u548c\u51fd\u6570\u3002<\/p>\n<p>\u4e0b\u9762\u8ba9\u6211\u4eec\u7528\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\uff1a\u6bd4\u5982\u4e00\u4e2a\u4eba\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002\u5c5e\u6027\u662f\u4e0e\u5bf9\u8c61\u6709\u8054\u7cfb\u7684\u503c\uff0c\u6bd4\u5982\u4eba\u7684\u5c5e\u6027\u5305\u62ec\u59d3\u540d\uff0c\u8eab\u9ad8\uff0c\u4f53\u91cd\uff0c\u5e74\u9f84\uff0c\u80a4\u8272\uff0c\u773c\u775b\u7684\u989c\u8272\u7b49\u7b49\u3002\u6240\u6709\u4eba\u90fd\u6709\u8fd9\u4e9b\u5c5e\u6027\uff0c\u4f46\u662f\u6bcf\u4e2a\u4eba\u7684\u5c5e\u6027\u503c\u90fd\u4e0d\u540c\u3002\u5bf9\u8c61\u8fd8\u6709\u51fd\u6570\uff0c\u51fd\u6570\u662f\u53ef\u4ee5\u5bf9\u5bf9\u8c61\u6267\u884c\u7684\u52a8\u4f5c\u3002\u6bd4\u5982\u4eba\u7684\u51fd\u6570\u53ef\u4ee5\u662f\u5403eat(), \u7761sleep(), \u5de5\u4f5cwork(), \u73a9play(), \u7b49\u7b49\u3002<\/p>\n<h3>\u5c5e\u6027<\/h3>\n<p>\u8bbf\u95ee\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u7684\u8bed\u6cd5\u662f\uff1a<\/p>\n<table class=\"ex\" id=\"table10\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\nobjName.propName\n<\/td>\n<\/tr>\n<\/table>\n<p>\u53ea\u8981\u8d4b\u503c\u5c31\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u7ed9\u5bf9\u8c61\u589e\u52a0\u4e00\u4e2a\u5c5e\u6027\u3002\u5047\u8bbe\u5bf9\u8c61personObj\u5df2\u7ecf\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u8c61\u4e0b\u9762\u8fd9\u6837\u7ed9\u5b83\u4e00\u4e9b\u5c5e\u6027\uff0c\u5305\u62ecfirstname,  lastname, age, \u548c eyecolor:<\/p>\n<table class=\"ex\" id=\"table11\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\npersonObj.firstname=&#8221;John&#8221;<br \/>\npersonObj.lastname=&#8221;Doe&#8221;<br \/>\npersonObj.age=30<br \/>\npersonObj.eyecolor=&#8221;blue&#8221;<br \/>\ndocument.write(personObj.firstname)\n<\/td>\n<\/tr>\n<\/table>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5c06\u4f1a\u8f93\u51fa\uff1a<\/p>\n<table class=\"ex\" id=\"table13\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\nJohn\n<\/td>\n<\/tr>\n<\/table>\n<h3>\u51fd\u6570<\/h3>\n<p>\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u6709\u51fd\u6570\uff0c\u8c03\u7528\u5bf9\u8c61\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<table class=\"ex\" id=\"table15\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\nobjName.methodName()\n<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u5907\u6ce8:<\/strong> \u51fd\u6570\u62ec\u53f7\u91cc\u9762\u53ef\u4ee5\u6709\u53c2\u6570\u3002<\/p>\n<p>\u8c03\u7528personObj\u7684\u51fd\u6570sleep()\u53ef\u4ee5\u8fd9\u6837\u5199:<\/p>\n<table class=\"ex\" id=\"table18\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\npersonObj.sleep()\n<\/td>\n<\/tr>\n<\/table>\n<hr \/>\n<h2>\u5b9a\u4e49\u81ea\u5df1\u7684\u5bf9\u8c61<\/h2>\n<p>\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff1a<\/p>\n<p><strong>1. \u76f4\u63a5\u751f\u6210\u4e00\u4e2a\u5bf9\u8c61\u7684\u5b9e\u4f8b<\/strong><\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u751f\u6210\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u5e76\u7ed9\u4e86\u5b834\u4e2a\u6570\u6027\uff1a<\/p>\n<table class=\"ex\" id=\"table19\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\npersonObj=new Object()<br \/>\npersonObj.firstname=&#8221;John&#8221;<br \/>\npersonObj.lastname=&#8221;Doe&#8221;<br \/>\npersonObj.age=50<br \/>\npersonObj.eyecolor=&#8221;blue&#8221;\n<\/td>\n<\/tr>\n<\/table>\n<p>\u7ed9\u5bf9\u8c61personObj\u589e\u52a0\u4e00\u4e2a\u51fd\u6570\u4e5f\u5f88\u7b80\u5355\uff0c\u5982\u4e0b\u4ee3\u7801\u5373\u53ef\u4e3a\u5b83\u589e\u52a0\u4e00\u4e2a\u53eb\u505a eat() \u7684\u51fd\u6570\uff1a<\/p>\n<table class=\"ex\" id=\"table20\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\npersonObj.eat=eat\n<\/td>\n<\/tr>\n<\/table>\n<p><strong>2. \u5b9a\u4e49\u4e00\u4e2a\u5bf9\u8c61\u7684\u6a21\u677f<\/strong><\/p>\n<p>\u6a21\u677f\u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u7ed3\u6784\uff1a<\/p>\n<table class=\"ex\" id=\"table23\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\nfunction person(firstname,lastname,age,eyecolor)<br \/>\n{<br \/>\nthis.firstname=firstname<br \/>\nthis.lastname=lastname<br \/>\nthis.age=age<br \/>\nthis.eyecolor=eyecolor<br \/>\n}\n<\/td>\n<\/tr>\n<\/table>\n<p>\u6ce8\u610f\u6a21\u677f\u5b9e\u9645\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u4f60\u9700\u8981\u5728\u51fd\u6570\u5185\u7ed9this.propertyName\u4e00\u4e00\u8d4b\u503c\u3002\u6bcf\u4e2a\u5c5e\u6027\u524d\u9762\u52a0\u5173\u952e\u5b57&#8221;this&#8221;\u7684\u539f\u56e0\u662f\u4f60\u5c06\u4f1a\u540c\u65f6\u6709\u591a\u4e2a\u5bf9\u8c61\u4eba\uff0c\u5fc5\u987b\u6307\u660e\u6b63\u5728\u5904\u7406\u54ea\u4e2a\u4eba\u4fe1\u606f\u3002\u201cthis\u201d\u7684\u542b\u4e49\u5c31\u662f\u6307\u624b\u5934\u6b63\u5728\u5904\u7406\u7684\u8fd9\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>\u4e00\u65e6\u5b9a\u4e49\u4e86\u6a21\u677f\uff0c\u5c31\u53ef\u4ee5\u7528\u5b83\u6765\u751f\u6210\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<table class=\"ex\" id=\"table24\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\nmyFather=new person(&#8220;John&#8221;,&#8221;Doe&#8221;,50,&#8221;blue&#8221;)<br \/>\nmyMother=new person(&#8220;Sally&#8221;,&#8221;Rally&#8221;,48,&#8221;green&#8221;)\n<\/td>\n<\/tr>\n<\/table>\n<p>\u5728\u6a21\u677f\u5185\u8fd8\u53ef\u4ee5\u589e\u52a0\u4e00\u4e9b\u51fd\u6570\uff0c\u6bd4\u5982\u4e0b\u9762\u7684newlastname\u51fd\u6570\uff1a<\/p>\n<table class=\"ex\" id=\"table25\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\nfunction person(firstname,lastname,age,eyecolor)<br \/>\n{<br \/>\nthis.firstname=firstname<br \/>\nthis.lastname=lastname<br \/>\nthis.age=age<br \/>\nthis.eyecolor=eyecolor<br \/>\nthis.newlastname=newlastname<br \/>\n}\n<\/td>\n<\/tr>\n<\/table>\n<p>\u6ce8\u610f\u4e0a\u9762\u5bf9\u8c61\u7684\u51fd\u6570\u53ea\u662f\u5b9a\u4e49\u4e86\u540d\u79f0\u4e0a\u7684\u8054\u7cfb\uff0c\u6211\u4eec\u4e0b\u9762\u8fd8\u8981\u5177\u4f53\u5199\u51fd\u6570 newlastname() \u7684\u5b9e\u73b0:<\/p>\n<table class=\"ex\" id=\"table8\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td valign=\"top\">\nfunction newlastname(new_lastname)<br \/>\n{<br \/>\nthis.lastname=new_lastname<br \/>\n}\n<\/td>\n<\/tr>\n<\/table>\n<p>\u6709\u4e86\u4ee5\u4e0a\u5b9a\u4e49\uff0c\u73b0\u5728\u4f60\u53ef\u4ee5\u901a\u8fc7\u5199  myMother.newlastname(&#8220;Doe&#8221;) \u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u4e86\u3002<\/p>\n<hr \/>\n<p> <a href=\"http:\/\/www.w3schools.com\/js\/js_timing.asp\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u5bf9\u8c61\u4fbf\u4e8e\u7ec4\u7ec7\u4fe1\u606f\u3002\u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u5982\u4f55\u5728JavaScript\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u7684\u5bf9\u8c61\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; JavaScript \u5bf9\u8c61 \u5728\u524d\u9762\u51e0\u7ae0\u4e2d\u6211\u4eec\u5b66\u5230JavaScript\u4e2d\u6709\u4e9b\u5185\u7f6e\u7684\u5bf9\u8c61\uff0c\u6bd4\u5982String, Date, Array\u7b49\u7b49\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5bf9\u8c61\u3002 \u5bf9\u8c61\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6570\u636e\uff0c\u542b\u6709\u5c5e\u6027\u548c\u51fd\u6570\u3002 \u4e0b\u9762\u8ba9\u6211\u4eec\u7528\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\uff1a\u6bd4\u5982\u4e00\u4e2a\u4eba\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002\u5c5e\u6027\u662f\u4e0e\u5bf9\u8c61\u6709\u8054\u7cfb\u7684\u503c\uff0c\u6bd4\u5982\u4eba\u7684\u5c5e\u6027\u5305\u62ec\u59d3\u540d\uff0c\u8eab\u9ad8\uff0c\u4f53\u91cd\uff0c\u5e74\u9f84\uff0c\u80a4\u8272\uff0c\u773c\u775b\u7684\u989c\u8272\u7b49\u7b49\u3002\u6240\u6709\u4eba\u90fd\u6709\u8fd9\u4e9b\u5c5e\u6027\uff0c\u4f46\u662f\u6bcf\u4e2a\u4eba\u7684\u5c5e\u6027\u503c\u90fd\u4e0d\u540c\u3002\u5bf9\u8c61\u8fd8\u6709\u51fd\u6570\uff0c\u51fd\u6570\u662f\u53ef\u4ee5\u5bf9\u5bf9\u8c61\u6267\u884c\u7684\u52a8\u4f5c\u3002\u6bd4\u5982\u4eba\u7684\u51fd\u6570\u53ef\u4ee5\u662f\u5403eat(), \u7761sleep(), \u5de5\u4f5cwork(), \u73a9play(), \u7b49\u7b49\u3002 \u5c5e\u6027 \u8bbf\u95ee\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u7684\u8bed\u6cd5\u662f\uff1a objName.propName \u53ea\u8981\u8d4b\u503c\u5c31\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u7ed9\u5bf9\u8c61\u589e\u52a0\u4e00\u4e2a\u5c5e\u6027\u3002\u5047\u8bbe\u5bf9\u8c61personObj\u5df2\u7ecf\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u8c61\u4e0b\u9762\u8fd9\u6837\u7ed9\u5b83\u4e00\u4e9b\u5c5e\u6027\uff0c\u5305\u62ecfirstname, lastname, age, \u548c eyecolor: personObj.firstname=&#8221;John&#8221; personObj.lastname=&#8221;Doe&#8221; personObj.age=30 personObj.eyecolor=&#8221;blue&#8221; document.write(personObj.firstname) \u4e0a\u9762\u7684\u4ee3\u7801\u5c06\u4f1a\u8f93\u51fa\uff1a John \u51fd\u6570 \u5bf9\u8c61\u4e5f\u53ef\u4ee5\u6709\u51fd\u6570\uff0c\u8c03\u7528\u5bf9\u8c61\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a objName.methodName() \u5907\u6ce8: \u51fd\u6570\u62ec\u53f7\u91cc\u9762\u53ef\u4ee5\u6709\u53c2\u6570\u3002 \u8c03\u7528personObj\u7684\u51fd\u6570sleep()\u53ef\u4ee5\u8fd9\u6837\u5199: personObj.sleep() \u5b9a\u4e49\u81ea\u5df1\u7684\u5bf9\u8c61 \u6709\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff1a 1. \u76f4\u63a5\u751f\u6210\u4e00\u4e2a\u5bf9\u8c61\u7684\u5b9e\u4f8b \u4e0b\u9762\u7684\u4ee3\u7801\u751f\u6210\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u5e76\u7ed9\u4e86\u5b834\u4e2a\u6570\u6027\uff1a personObj=new Object() personObj.firstname=&#8221;John&#8221; personObj.lastname=&#8221;Doe&#8221; personObj.age=50 personObj.eyecolor=&#8221;blue&#8221; \u7ed9\u5bf9\u8c61personObj\u589e\u52a0\u4e00\u4e2a\u51fd\u6570\u4e5f\u5f88\u7b80\u5355\uff0c\u5982\u4e0b\u4ee3\u7801\u5373\u53ef\u4e3a\u5b83\u589e\u52a0\u4e00\u4e2a\u53eb\u505a eat() \u7684\u51fd\u6570\uff1a personObj.eat=eat 2. \u5b9a\u4e49\u4e00\u4e2a\u5bf9\u8c61\u7684\u6a21\u677f \u6a21\u677f\u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u7ed3\u6784\uff1a function person(firstname,lastname,age,eyecolor) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":31,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/44"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/44\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}