{"id":1162,"date":"2010-10-14T21:34:53","date_gmt":"2010-10-14T13:34:53","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1162"},"modified":"2018-01-01T19:44:02","modified_gmt":"2018-01-01T11:44:02","slug":"%e7%bf%bb%e8%af%91-%e8%a7%a3%e9%87%8ajavascript%e7%9a%84%e2%80%9c%e9%a2%84%e8%a7%a3%e6%9e%90%e7%bd%ae%e9%a1%b6%e8%a7%a3%e6%9e%90%e2%80%9d","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2010\/10\/%e7%bf%bb%e8%af%91-%e8%a7%a3%e9%87%8ajavascript%e7%9a%84%e2%80%9c%e9%a2%84%e8%a7%a3%e6%9e%90%e7%bd%ae%e9%a1%b6%e8%a7%a3%e6%9e%90%e2%80%9d\/","title":{"rendered":"\u7ffb\u8bd1 &#8211; \u89e3\u91caJavaScript\u7684\u201c\u9884\u89e3\u6790(\u7f6e\u9876\u89e3\u6790)\u201d"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a><br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1162\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=1162<\/a><\/p>\n<div id=\"link\">\n\t\u539f\u6587\u94fe\u63a5\uff1a<a target=\"_blank\" href=\"http:\/\/net.tutsplus.com\/tutorials\/javascript-ajax\/quick-tip-javascript-hoisting-explained\/\">Quick Tip: JavaScript Hoisting Explained <\/a><br \/>\n    \u539f\u6587\u4f5c\u8005\uff1a<a target=\"_blank\" title=\"Jeffrey Way\" href=\"http:\/\/net.tutsplus.com\/author\/jeffreyway\/\">Jeffrey Way<\/a><br \/>\n    \u7ffb\u8bd1\u7f16\u8f91\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/\">\u5f20\u946b\u65ed<\/a>\n<\/div>\n<p>\u524d\u4e24\u5929\u6211\u53c8\u4e00\u6b21\u770b\u4e86<a target=\"_blank\" href=\"http:\/\/lifesinger.org\/blog\/\">\u7389\u4f2f<\/a>\u7684<a target=\"_blank\" href=\"http:\/\/lifesinger.org\/blog\/2009\/01\/javascript-run-mechanism\/\">JavaScript\u8fd0\u884c\u673a\u5236\u6d45\u63a2<\/a>\uff0c\u4ee5\u52a0\u6df1\u81ea\u5df1\u5bf9JavaScript\u8fd0\u884c\u539f\u7406\u65b9\u9762\u7684\u4e00\u4e9b\u7406\u89e3\u3002\u6070\u597d\uff0c\u4eca\u5929\u89c1\u5230\u4e00\u7bc7\u56fd\u5916\u7684\u6587\u7ae0\uff0c\u5c31\u662f\u5173\u4e8e\u201c\u9884\u89e3\u6790\u201d\u7684\uff0c\u8fd8\u6709\u89c6\u9891\u5185\u5bb9\uff0c\u4e8e\u662f\u51b3\u5b9a\u7ffb\u8bd1\u4e0b\uff0c\u5bf9\u81ea\u5df1\u800c\u8a00\u7b97\u662f\u8fdb\u4e00\u6b65\u7684\u68b3\u7406\u4e0e\u52a0\u6df1\u8ba4\u8bc6\uff0c\u4ee5\u4e0b\u5c31\u662f\u7ffb\u8bd1\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u4e3a\u4e86\u8bed\u8a00\u7684\u751f\u52a8\u4ee5\u53ca\u672c\u571f\u5316\u8981\u6c42\uff0c\u5185\u5bb9\u53ef\u80fd\u6709\u6240\u7f16\u8f91\u3002<\/p>\n<p><span class=\"s\">\/\/zxx:\u4ee5\u4e0b\u4e3a\u7ffb\u8bd1\u5168\u90e8\u5185\u5bb9<\/span><\/p>\n<h3>\u4e00\u3001\u6839\u6b63\u82d7\u7ea2\u7684\u5f15\u5b50<\/h3>\n<p>\u4eca\u5929\u8fd9\u4e2a\u89c6\u9891\u8bde\u751f\u7684\u539f\u56e0\u662f\u7531\u4e8e\u56de\u7b54<a href=\"http:\/\/www.twitter.com\/\">twitter<\/a>\u4e0a\u7684\u4e00\u4e2a\u5173\u4e8eJavaScript\u201c\u9884\u89e3\u6790\u201d\u7684\u95ee\u9898\u3002<span class=\"s\">\/\/zxx:\u539f\u5355\u8bcd\u4e3a&#8221;hoisting&#8221;\uff0c\u6211\u7ffb\u8bd1\u6210\u4e86\u201c\u9884\u89e3\u6790\u201d\uff0c\u82e5\u6709\u4e0d\u51c6\uff0c\u6b22\u8fce\u6307\u6b63\u3002<\/span><br \/>\u90a3\u4e48\u4ec0\u4e48\u662f\u201c\u9884\u89e3\u6790\u201d\uff1f\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\u4f60\u9700\u8981\u77e5\u9053\u5b83\u7684\u90a3\u4e9b\u5c0f\u79d8\u5bc6\u5462\uff1f\u6240\u6709\u8fd9\u4e9b\u95ee\u9898\u7684\u56de\u7b54\u53ef\u4ee5\u4ece\u4e0b\u9762\u8fd9\u4e2a\u6bd4\u8f83\u57fa\u7840\u7684\u89c6\u9891\u6559\u7a0b\u4e2d\u627e\u5230\u3002<\/p>\n<p style=\"width:560px; height:345px; background:#000;\"><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http:\/\/screenr.com\/Content\/assets\/screenr_1116090935.swf' \/><param name='flashvars' value='i=115493' \/><param name='allowFullScreen' value='true' \/><embed src='http:\/\/screenr.com\/Content\/assets\/screenr_1116090935.swf' flashvars='i=115493' allowFullScreen='true' width='560' height='345' pluginspage='http:\/\/www.macromedia.com\/go\/getflashplayer'><\/embed><\/object><\/p>\n<h3>\u4e8c\u3001\u201c\u9884\u89e3\u6790\u201d\u89e3\u91ca<\/h3>\n<p>\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>\nvar myvar = 'my value';\nalert(myvar); <span style=\"color:green\">\/\/ my value<\/span>\n<\/pre>\n<\/div>\n<p>Okay, \u5f53\u7136\uff0c\u5f39\u51fa\u7684\u7ed3\u679c\u80af\u5b9a\u662f<code>\"my value\"<\/code>\uff0c\u4f46\u662f\uff0c\u8ddf\u7740\u6211\uff0c\u8ba9\u6211\u4e0b\u9762\u521b\u5efa\u4e2a\u65b9\u6cd5\uff0c\u5f39\u51fa\u76f8\u540c\u7684\u503c\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>\nvar myvar = 'my value';\n\n(function() {\n  alert(myvar); <span style=\"color:green\">\/\/ my value<\/span>\n})();\n\n<\/pre>\n<\/div>\n<p>\u597d\u5427\uff0c\u597d\u5427\uff0c\u4ecd\u7136\u5f88\u660e\u663e\uff0c\u6211\u77e5\u9053\u3002\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u52a0\u70b9\u731b\u7684\u8c03\u6599\uff0c\u5728\u533f\u540d\u51fd\u6570\u5185\u90e8\u521b\u5efa\u4e00\u4e2a\u540c\u540d\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>\nvar myvar = 'my value';\n\n(function() {\n  alert(myvar); <span style=\"color:green\">\/\/ undefined<\/span>\n  var myvar = 'local value';\n})();\n<\/pre>\n<\/div>\n<p>\u54c8\u554a\uff1f\u4e3a\u4ec0\u4e48\u5f39\u51fa\u7684\u662f<code>undefined<\/code>\u5462\uff1f\u5373\u4f7f\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u4f46\u662f\u5b83\u5728alert\u7684\u4e0b\u9762\u554a\uff0c\u7167\u7406\u8bf4\u5e94\u8be5\u4e0d\u4f1a\u6709\u5f71\u54cd\u554a\uff0c\u662f\u5417\uff1f\u8fd8\u662f\uff1f<\/p>\n<h3>\u4e09\u3001\u53d8\u91cf\u58f0\u660e\u88ab\u201c\u9884\u89e3\u6790\u201d<\/h3>\n<p>\u5728\u5f53\u524d\u7684\u4f5c\u7528\u57df\u5185\uff0c\u65e0\u8bba\u5728\u54ea\u91cc\u53d8\u91cf\u58f0\u660e\uff0c\u5728\u5e55\u540e\uff0c\u5176\u90fd\u5728\u9876\u90e8\u88ab\u201c\u9884\u89e3\u6790\u201d\u4e86\u3002\u4e0d\u8fc7\uff0c\u4ec5\u58f0\u660e\u88ab\u201c\u9884\u89e3\u6790\u201d\u3002\u8be5\u53d8\u91cf\u5373\u4f7f\u521d\u59cb\u5316\uff0c\u5176\u5f53\u524d\u7684\u503c\uff0c\u5728\u4f5c\u7528\u57df\u7684\u9876\u90e8\uff0c\u4e5f\u4f1a\u88ab\u8bbe\u7f6e\u6210<code>undefined<\/code>\u3002<\/p>\n<p>\u6069\uff0c\u73b0\u5728\u8ba9\u6211\u4eec\u597d\u597d\u7684\u7834\u8bd1\u4e0b\u8fd9\u4e2a\u201c\u58f0\u660e\u201d\u548c\u201c\u521d\u59cb\u5316\u201d\uff0c\u4ee5<code>var joe = 'plumber';<\/code>\u4e3a\u6a21\u7279\u5427\u3002<\/p>\n<p><strong>\u58f0\u660e(Declaration)<\/strong><\/p>\n<div class=\"zxx_code\">\n<pre>\nvar joe; <span style=\"color:green\">\/\/ the declaration<\/span>\n<\/pre>\n<\/div>\n<p><strong>\u521d\u59cb\u5316(Initialization)<\/strong><\/p>\n<div class=\"zxx_code\">\n<pre>\njoe = 'plumber'; <span style=\"color:green\">\/\/ the initialization <\/span>\n<\/pre>\n<\/div>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u77e5\u9053\u4e86\u8fd9\u4e9b\u672f\u8bed\u7684\u610f\u601d\uff0c\u5c31\u53ef\u4ee5\u66f4\u597d\u7684\u7406\u89e3\u5230\u5e95\u80cc\u5730\u91cc\u90fd\u5e72\u4e86\u4e9b\u4ec0\u4e48\u52fe\u5f53\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u4f2a\u51fd\u6570\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>\n(function() {\n  var a = 'a';\n  <span style=\"color:green\">\/\/ \u4e00\u884c\u4ee3\u7801<\/span>\n  var b = 'b';\n  <span style=\"color:green\">\/\/ \u66f4\u591a\u884c\u7684\u4ee3\u7801<\/span>\n  var c= 'c'; <span style=\"color:green\">\/\/ antipattern<\/span>\n  <span style=\"color:green\">\/\/ \u6700\u540e\u4e00\u884c\u811a\u672c<\/span>\n})();\n<\/pre>\n<\/div>\n<p>\u9700\u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u8fd9\u505a\u6cd5\u662f\u4e0d\u592a\u597d\u7684\u3002\u4f46\u662f\uff0c\u5148\u4e0d\u7ba1\u8fd9\u4e2a\uff0c\u5728\u7a0b\u5e8f\u7684\u80cc\u540e\uff0c\u8fd9\u4e2a\u53d8\u91cf\u58f0\u660e\u65e0\u8bba\u5728\u51fd\u6570\u4f5c\u7528\u57df\u7684\u4ec0\u4e48\u5730\u65b9\uff0c\u90fd\u88ab\u7f6e\u9876\u89e3\u6790\u4e86\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>\n(function() {\n  var a, b, c; \/\/ variables declared\n  a = 'a';\n  <span style=\"color:green\">\/\/ \u4e00\u884c\u4ee3\u7801<\/span>\n  b = 'b'; \/\/ initialized\n  <span style=\"color:green\">\/\/ \u66f4\u591a\u884c\u7684\u4ee3\u7801<\/span>\n  c= 'c'; \/\/ initialized\n  <span style=\"color:green\">\/\/ \u6700\u540e\u4e00\u884c\u811a\u672c<\/span>\n})();\n<\/pre>\n<\/div>\n<h3>\u56db\u3001soga\uff01<\/h3>\n<p>\u73b0\u5728\u56de\u5230\u4ee4\u4eba\u7591\u60d1\u7684<code>undefined<\/code>\u4ee3\u7801\u6bb5<\/p>\n<div class=\"zxx_code\">\n<pre>\nvar myvar = 'my value';\n\n(function() {\n  alert(myvar); <span style=\"color:green\">\/\/ undefined<\/span>\n  var myvar = 'local value';\n})();\n<\/pre>\n<\/div>\n<p>\u73b0\u5728\u5e94\u8be5\u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\u4e3a\u4f55<code>myvar<\/code>\u5f39\u51fa\u7684\u662f<code>undefined<\/code>\u4e86\u3002\u6b63\u5982\u4e0a\u9762\u5b66\u5230\u4e86\uff0c\u5f53\u6211\u4eec\u58f0\u660e<code>myvar<\/code>\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u5176\u81ea\u52a8\u7684\u5728\u51fd\u6570\u4f5c\u7528\u57df\u7684\u9876\u90e8\uff08\u5728alert\u7684\u524d\u9762\uff09\u8fdb\u884c\u201c\u89e3\u6790\u201d\u4e86\u3002\u7ed3\u679c\uff0c\u5728alert\u7684\u65f6\u5019\u65f6\u7684\u53d8\u91cf\u8fd8\u6ca1\u6709\u88ab\u58f0\u660e\u3002\u7136\u800c\uff0c\u7531\u4e8e\u521d\u59cb\u5316\u7684\u503c\u662f\u4e0d\u4f1a\u4f5c\u7528\u57df\u7f6e\u9876\u7684\uff0c\u6240\u4ee5\uff0c\u6700\u540ealert\u7684\u503c\u662f<code>undefined<\/code>\u3002<\/p>\n<p><span class=\"s\">\/\/zxx:\u4ee5\u4e0a\u4e3a\u7ffb\u8bd1\u5168\u6587<\/span><\/p>\n<h3>\u8bd1\u8005\u8865\u5145\uff1a<\/h3>\n<p>\u4e0a\u9762\u5f39\u51fa<code>undefined<\/code>\u7684\u4ee3\u7801\u5b9e\u9645\u4e0a\u5c31\u7b49\u6548\u4e8e\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>\nvar myvar = 'my value';\n\n(function() {\n  var myvar;\n  alert(myvar); <span style=\"color:green\">\/\/ undefined<\/span>\n  myvar = 'local value';\n})();\n<\/pre>\n<\/div>\n<p>JavaScript\u7684\u9884\u89e3\u6790\u4e0d\u4ec5\u9002\u7528\u4e8e\u53d8\u91cf\uff0c\u4f3c\u4e4e\u4e5f\u9002\u7528\u4e8e\u51fd\u6570\uff0c\u4f60\u6709\u5174\u8da3\u53ef\u4ee5\u770b\u4e0b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>\ng = function() { return false; };\n(function() {\n    if(g()) {\n        function g() { return true; }\n        alert(\"in\"); <span style=\"color:green\">\/\/IE6~8\uff0cChrome\uff0cSafari\uff0cOpera\u6d4f\u89c8\u5668\u6267\u884c<\/span>\n    }\n})();\n<\/pre>\n<\/div>\n<p>\u5728\u975eFirefox\u6d4f\u89c8\u5668\u4e0b\uff0c\u4f1a\u5f39\u51fa\u5b57\u7b26\u4e32<code>\"in\"<\/code>\u7684\uff0c\u5f53\u7136\uff0c\u8fd9\u91cc\u7684\u91cd\u70b9\u4e0d\u662f\u54ea\u4e2a\u6d4f\u89c8\u5668\u6267\u4e0d\u6267\u884c\uff0c\u6240\u4ee5\u4e0d\u8003\u8651Firefox\u7684\u95ee\u9898\uff0c<code>function g() { return true; }<\/code>\u542b\u51fd\u6570\u4f5c\u7528\u57df\u9876\u90e8\uff08\u5728if\u7684\u524d\u9762\uff09\u201c\u9884\u89e3\u6790\u201d\uff0c\u6240\u4ee5\uff0cif\u8bed\u53e5\u53ef\u4ee5\u6267\u884c\u3002<\/p>\n<p>\u672c\u6587\u6d89\u53ca\u5230JavaScript\u4e00\u4e9b\u8fd0\u884c\u673a\u5236\uff0c\u672c\u8eab\u8d44\u8d28\u4e5f\u6709\u9650\uff0c\u6240\u4ee5\u6587\u7ae0\u53ef\u80fd\u6709\u7ffb\u8bd1\u6216\u662f\u8868\u8ff0\u4e0d\u51c6\u786e\u7684\u5730\u65b9\uff0c\u8fd8\u671b\u4e0d\u9057\u4f59\u529b\u7684\u6307\u6b63\uff0c\u4e0d\u751a\u611f\u8c22\u3002<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u81ea<a href=\"http:\/\/www.zhangxinxu.com\/\">\u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<\/a>[<a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a>]<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1162\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=1162<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u53ef\u4ee5\u8bf4\u662f\u4e2a\u5c0f\u7684\u6559\u7a0b\uff0c\u5206\u6790\u4e00\u4e9bJavaScript\u201c\u9884\u89e3\u6790\u201d\u9020\u6210\u7684\u53ef\u80fd\u4f1a\u8ba9\u4eba\u7591\u60d1\u7684\u73b0\u8c61\u3002\u867d\u8bf4\u662f\u7ffb\u8bd1\u6587\u7ae0\uff0c\u4f46\u662f\u4e2a\u4eba\u611f\u89c9\u8fd8\u662f\u86ee\u901a\u4fd7\u6613\u61c2\u7684\u3002\u672c\u7740\u81ea\u5df1\u5728JavaScript\u4e0a\u7684\u9020\u8be3\u5b9e\u5728\u6709\u9650\uff0c\u52a0\u4e0a\u53c8\u662f\u6709\u5173JavaScript\u8fd0\u884c\u673a\u5236\u8fd9\u7c7b\u6bd4\u8f83\u6df1\u5c42\u6b21\u7684\u95ee\u9898\uff0c\u5728\u628a\u63e1\u4e0a\u5408\u7406\u89e3\u4e0a\u96be\u514d\u6709\u4e00\u4e9b\u4e0d\u5230\u4f4d\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u5404\u4f4d\u4f18\u79c0\u7684\u540c\u884c\u591a\u591a\u6307\u6b63\uff0c\u591a\u591a\u8d50\u6559\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198],"tags":[13,504,140,505,503,139,223,502],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/1162"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1162"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/1162\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}