{"id":4794,"date":"2016-02-21T23:48:25","date_gmt":"2016-02-21T15:48:25","guid":{"rendered":"https:\/\/sdeno.com\/?p=4794"},"modified":"2016-02-22T00:21:48","modified_gmt":"2016-02-21T16:21:48","slug":"%e5%9c%a8js%e4%b8%ad%e4%b8%ba%e4%bb%80%e4%b9%88%e8%af%b4%e5%87%bd%e6%95%b0%e4%b9%9f%e6%98%af%e5%af%b9%e8%b1%a1%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=4794","title":{"rendered":"\u8bf7\u95eejs\u4e2d\u7ed9function\u6dfb\u52a0\u7684\u5c5e\u6027\u53bb\u54ea\u513f\u4e86\uff1f\u6709\u4ec0\u4e48\u7528\u9014\uff1f\uff0c\u5728js\u4e2d\u4e3a\u4ec0\u4e48\u8bf4\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff1f"},"content":{"rendered":"<p>\u51fd\u6570\u662f\u5bf9\u8c61<\/p>\n<p>\u5728js\u4e2d\u4e3a\u4ec0\u4e48\u8bf4\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff1f<\/p>\n<p>\u6211\u4eec\u90fd\u77e5\u9053js\u4e2d\u7684\u5bf9\u8c61\u8981\u8c03\u7528\u91cc\u9762\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u4e00\u822c\u7684\u5199\u6cd5\u90fd\u662f\uff1a<\/p>\n<pre>obj,name; \u00a0\r\nobj.run();<\/pre>\n<p>\u8fd9\u6837\u5f88\u6b63\u5e38\uff0c\u4f46\u662ffunction\u4e5f\u80fd\u8fd9\u4e48\u5199\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>function wo(){} \u00a0\/\/\u58f0\u660e\u4e86\u4e00\u4e2a\u51fd\u6570\r\n\r\nwo.name='\u6211';\r\nwo.run=function(){ return '\u8fd9\u662f\u4e00\u4e2a\u65b9\u6cd5'; }\r\n\r\n\u8c03\u7528\u65b9\u6cd5\u8ddf\u8c03\u7528\u5bf9\u8c61\u4e00\u6837\uff1a\r\nobj.name; \r\nobj.run();<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5176\u5b9e\u8fd9\u4e48\u5199\u7684\u4f5c\u7528\uff0c\u5c31\u8981\u8054\u60f3\u5230js\u7684\u9762\u5411\u5bf9\u8c61\u601d\u60f3\u4e86\uff0c\u4e00\u822c\u6211\u4eec\u5199\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u5199\u6cd5\u662f\uff1a<\/p>\n<pre>function Wo(){\r\n this.myname='\u62111'; \/\/\u8fd9\u91cc\u7684this\u5176\u5b9e\u4e5f\u5c31\u662fWo\u672c\u8eab,\u5e76\u4e14\u8fd8\u662f<span style=\"color: #ff0000;\">\u79c1\u6709\u7684<\/span>,\u5916\u754c\u65e0\u6cd5\u8bbf\u95ee\uff0c\u4e5f\u4e0d\u80fd\u4fee\u6539\uff0c\u540c\u65f6\u4e5f\u662f\u8d77\u5230\u4e86<span style=\"color: #ff0000;\">\u521d\u59cb\u5316\u7684\u4f5c\u7528<\/span>\u3002\r\n this.run=function(){ }\r\n}\r\n\r\nalert( Wo.myname ); \/\/\u8bd5\u56fe\u8bbf\u95ee myname \u8fd4\u56de\u7684\u662f undefined\r\n\r\nWo.myname='\u62112'; \/\/\u8bd5\u56fe\u4fee\u6539 this.myname\u7684\u503c\r\n\r\nalert( Wo.myname ); \/\/\u4fee\u6539\u5931\u8d25\uff0c\u56e0\u4e3a\u4e0d\u80fd\u8bbf\u95ee\uff0c\u8fd9\u91cc\u8bbf\u95ee\u5230\u7684\u662f\u5916\u754c\u7684 \u62112<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5f15\u5165\u4e00\u7bc7\u6587\u7ae0\uff1a\u300a<a href=\"https:\/\/segmentfault.com\/q\/1010000002727781\" target=\"_blank\">\u8bf7\u95eejs\u4e2d\u7ed9function\u6dfb\u52a0\u7684\u5c5e\u6027\u53bb\u54ea\u513f\u4e86\uff1f\u6709\u4ec0\u4e48\u7528\u9014\uff1f<\/a>\u300b<\/p>\n<p>\u8bf7\u95ee\u4e0b\uff0c\u56e0\u4e3aFunction\u662f\u5f15\u7528\u7c7b\u578b\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u7ed9\u4ed6\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u5c5e\u6027\uff0c\u53ef\u662f\u6dfb\u52a0\u7684\u8fd9\u4e2a\u5c5e\u6027\u6709\u4ec0\u4e48\u7528\u9014\u5462\uff1f<\/p>\n<div class=\"widget-codetool\"><button class=\"selectCode btn btn-xs\" style=\"font-weight: 400; font-style: inherit; color: #ffffff;\">\u5168\u9009<\/button><button class=\"copyCode btn btn-xs\" style=\"font-weight: 400; font-style: inherit; color: #ffffff;\" title=\"\" data-clipboard-text=\"var sfFunc = function(){};\nsfFunc.ename = 'segmentfault';\n\" data-toggle=\"tooltip\" data-placement=\"top\">\u590d\u5236<\/button><button class=\"saveToNote btn btn-xs\" style=\"font-weight: 400; font-style: inherit; color: #ffffff;\">\u653e\u8fdb\u7b14\u8bb0<\/button><\/div>\n<pre><code class=\"javascript\" style=\"color: inherit;\"><span class=\"hljs-keyword\" style=\"color: #859900;\">var<\/span> sfFunc = <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #859900;\">function<\/span><span class=\"hljs-params\">()<\/span><\/span>{};\r\nsfFunc.ename = <span class=\"hljs-string\" style=\"color: #2aa198;\">'segmentfault'<\/span>;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728javascript\u4e2d\uff0c\u51fd\u6570\u88ab\u8ba4\u4e3a\u662f\u7b2c\u4e00\u7c7b\u5bf9\u8c61\uff0c\u5373\u51fd\u6570\u53ef\u4ee5\u88ab\u4f20\u9012\u6216\u8005\u53ef\u4ee5\u643a\u5e26\u5c5e\u6027\u6216\u65b9\u6cd5\u3002<\/p>\n<p>\u5728\u51fd\u6570\u4e2d\u4f7f\u7528this\uff0c\u6dfb\u52a0\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u88ab\u8ba4\u4e3a\u662f\u4e3a\u5176\u6dfb\u52a0\u4e86\u79c1\u6709\u65b9\u6cd5\uff1a<br \/>\n(\u4f60\u4e3e\u4f8b\u7528\u4e86\u5bf9\u8c61\u5b57\u9762\u91cf\u521b\u5efa\u51fd\u6570\uff0c\u5728\u6dfb\u52a0\u5c5e\u6027\u8fd9\u65b9\u9762\u8ddf\u76f4\u63a5\u58f0\u660e\u51fd\u6570\u4e00\u81f4)<\/p>\n<pre class=\"hljs javascript\" style=\"color: #657b83;\"><code style=\"color: inherit;\"><span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #859900;\">function<\/span> <span class=\"hljs-title\" style=\"color: #268bd2;\">Bar<\/span><span class=\"hljs-params\">()<\/span><\/span>{\r\n    <span class=\"hljs-keyword\" style=\"color: #859900;\">this<\/span>.foo = <span class=\"hljs-number\" style=\"color: #2aa198;\">123<\/span>;\r\n}\r\n\r\n<span class=\"hljs-keyword\" style=\"color: #859900;\">var<\/span> temp = Bar;\r\ntemp.foo   <span class=\"hljs-comment\" style=\"color: #93a1a1;\">\/\/ undefined    \u8fd9\u6837\u662f\u8bbf\u95ee\u4e0d\u5230\u7684\uff0c\u867d\u7136temp\u6307\u5411\u4e86Bar\u7684\u5f15\u7528<\/span>\r\n\r\n<span class=\"hljs-keyword\" style=\"color: #859900;\">var<\/span> bar = <span class=\"hljs-keyword\" style=\"color: #859900;\">new<\/span> Bar();\r\nbar.foo    <span class=\"hljs-comment\" style=\"color: #93a1a1;\">\/\/ 123       \u56e0\u4e3abar\u662fBar\u7684\u5b9e\u4f8b\uff0c\u6240\u4ee5\u53ef\u4ee5\u7406\u89e3\u4e3abar\u5c06Bar\u91cc\u7684\u79c1\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u90fd\u514b\u9686\u5230\u81ea\u5df1\u7684\u8eab\u4e0a\uff0c\u5e76\u4e14\u514b\u9686\u8fc7\u6765\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u548c\u88ab\u514b\u9686\u4f53(Bar)\u8131\u79bb\u5e72\u7cfb\u3002<\/span>\r\n<\/code><\/pre>\n<p>\u79c1\u6709\u5316\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u4f5c\u7528\u662f\u4fdd\u62a4\u5c5e\u6027\u6216\u65b9\u6cd5\u4e0d\u88ab\u968f\u610f\u91cd\u5199\/\u8986\u76d6\uff1a<\/p>\n<pre class=\"hljs javascript\" style=\"color: #657b83;\"><code style=\"color: inherit;\"><span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #859900;\">function<\/span> <span class=\"hljs-title\" style=\"color: #268bd2;\">Blog<\/span><span class=\"hljs-params\">()<\/span><\/span>{\r\n    <span class=\"hljs-keyword\" style=\"color: #859900;\">this<\/span>.title = <span class=\"hljs-string\" style=\"color: #2aa198;\">'blue sky'<\/span>;\r\n}\r\n\r\n<span class=\"hljs-keyword\" style=\"color: #859900;\">var<\/span> bar = <span class=\"hljs-keyword\" style=\"color: #859900;\">new<\/span> Blog();\r\nbar.title      <span class=\"hljs-comment\" style=\"color: #93a1a1;\">\/\/ 'blue sky'<\/span>\r\n\r\n<span class=\"hljs-keyword\" style=\"color: #859900;\">var<\/span> foo = Blog;\r\nfoo.title = <span class=\"hljs-string\" style=\"color: #2aa198;\">'green sky'<\/span>    <span class=\"hljs-comment\" style=\"color: #93a1a1;\">\/\/ \u8bd5\u56fe\u6539\u5199title\u5c5e\u6027<\/span>\r\nfoo.title   <span class=\"hljs-comment\" style=\"color: #93a1a1;\">\/\/   'green sky'    <\/span>\r\nbar.title   <span class=\"hljs-comment\" style=\"color: #93a1a1;\">\/\/   'blue sky'    \u4f9d\u65e7\u662f\u539f\u6765\u7684\u503c<\/span>\r\n<\/code><\/pre>\n<p>\u90a3\u4e48\u8fd9\u6837\u6709\u4ec0\u4e48\u7528\u5462\uff1f<\/p>\n<p>\u6700\u5e38\u89c1\u5230\u7684\u5e94\u8be5\u5c31\u662f\u521d\u59cb\u5316\u5bf9\u8c61\u4e86\uff1a<\/p>\n<pre class=\"hljs javascript\" style=\"color: #657b83;\"><code style=\"color: inherit;\"><span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #859900;\">function<\/span> <span class=\"hljs-title\" style=\"color: #268bd2;\">People<\/span><span class=\"hljs-params\">(name,age)<\/span><\/span>{\r\n    <span class=\"hljs-keyword\" style=\"color: #859900;\">this<\/span>.name = name;\r\n    <span class=\"hljs-keyword\" style=\"color: #859900;\">this<\/span>.age = age;\r\n}\r\n\r\n<span class=\"hljs-keyword\" style=\"color: #859900;\">var<\/span> boy = <span class=\"hljs-keyword\" style=\"color: #859900;\">new<\/span> People(<span class=\"hljs-string\" style=\"color: #2aa198;\">'qianjiahao'<\/span>,<span class=\"hljs-number\" style=\"color: #2aa198;\">22<\/span>);\r\nboy.name   <span class=\"hljs-comment\" style=\"color: #93a1a1;\">\/\/ qianjiahao<\/span>\r\nboy.age    <span class=\"hljs-comment\" style=\"color: #93a1a1;\">\/\/ 22<\/span>\r\n<\/code><\/pre>\n<p>\u5b83\u53ef\u4ee5\u50cf\u5de5\u5382\u4e00\u6837\u6309\u4e00\u4e2a\u6a21\u5b50\uff0c\u7ed9\u4f60\u5236\u9020\u4efb\u610f\u591a\u7684\u56fa\u5b9a\u683c\u5f0f\u7684\u5bf9\u8c61\uff0c\u4f46\u662f\u7f3a\u9677\u4e5f\u5f88\u660e\u663e\uff0c\u683c\u5f0f\u592a\u56fa\u5b9a\uff0c\u5982\u679c\u60f3\u53d8\u53d8\u82b1\u6837\u8fd8\u4e0d\u884c\uff0c\u8fd9\u65f6\uff0c\u5c31\u53ef\u4ee5\u914d\u5408\u539f\u578b\uff0c\u6765\u5236\u9020\u4efb\u610f\u591a\u7684\u671f\u671b\u5bf9\u8c61\uff08\u6a21\u62df\u8bbe\u8ba1\u6a21\u5f0f\uff1a\u7ee7\u627f\uff09<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u662f\u5bf9\u8c61 \u5728js\u4e2d\u4e3a\u4ec0\u4e48\u8bf4\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff1f \u6211\u4eec\u90fd\u77e5\u9053js\u4e2d\u7684\u5bf9\u8c61\u8981\u8c03\u7528\u91cc\u9762\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u4e00\u822c\u7684\u5199\u6cd5\u90fd\u662f\uff1a ob [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,11],"tags":[],"class_list":["post-4794","post","type-post","status-publish","format-standard","hentry","category-javascript","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/4794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4794"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/4794\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}