{"id":624,"date":"2019-06-23T14:57:42","date_gmt":"2019-06-23T06:57:42","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=624"},"modified":"2019-11-23T15:25:24","modified_gmt":"2019-11-23T07:25:24","slug":"%e5%8f%98%e9%87%8f%e6%8f%90%e5%8d%87-%e5%87%bd%e6%95%b0%e6%8f%90%e5%8d%87%e8%bd%ac","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=624","title":{"rendered":"\u53d8\u91cf\u63d0\u5347-\u51fd\u6570\u63d0\u5347(\u8f6c)"},"content":{"rendered":"<h1>\u53d8\u91cf\u63d0\u5347<\/h1>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u51fd\u6570\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cf\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u58f0\u660e\u662f\u5728if\u8bed\u53e5\u5757\u4e2d\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>function hoistVariable() {\r\n    if (!foo) {\r\n        var foo = 5;\r\n    }\r\n\r\n    console.log(foo); \/\/ 5\r\n}\r\n\r\nhoistVariable();<\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>\u8fd0\u884c\u4ee3\u7801\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0foo\u7684\u503c\u662f5\uff0c<\/p>\n<p>\u90a3\u4e48\u81f3\u4e8e\u8bf4\u6253\u5370\u7ed3\u679c\uff0c\u7ecf\u8fc7\u4e00\u6b21\b\u9884\u7f16\u8bd1\u4e4b\u540e\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>\/\/ \u9884\u7f16\u8bd1\u4e4b\u540e\r\nfunction hoistVariable() {\r\n    var foo;\r\n\r\n    if (!foo) {\r\n        foo = 5;\r\n    }\r\n\r\n    console.log(foo); \/\/ 5\r\n}\r\n\r\nhoistVariable();<\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>\u5f15\u64ce\u5c06\u53d8\u91cf\u58f0\u660e\u63d0\u5347\u5230\u4e86\u51fd\u6570\u9876\u90e8\uff0c\u521d\u59cb\u503c\u4e3aundefined\uff0c\u81ea\u7136\uff0cif\u8bed\u53e5\u5757\u5c31\u4f1a\u88ab\u6267\u884c\uff0cfoo\u53d8\u91cf\u8d4b\u503c\u4e3a5\uff0c\u4e0b\u9762\u7684\u6253\u5370\u4e5f\u5c31\u662f\u9884\u671f\u7684\u7ed3\u679c\u4e86\u3002<\/p>\n<p>\u7c7b\u4f3c\u7684\uff0c\u8fd8\u6709\u4e0b\u9762\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre>var foo = 3;\r\n\r\nfunction hoistVariable() {\r\n    var foo = foo || 5;\r\n\r\n    console.log(foo); \/\/ 5\r\n}\r\n\r\nhoistVariable();<\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>foo || 5\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u662f5\u800c\u4e0d\u662f3\uff0c\u867d\u7136\u5916\u5c42\u4f5c\u7528\u57df\u6709\u4e2afoo\u53d8\u91cf\uff0c\u4f46\u51fd\u6570\u5185\u662f\u4e0d\u4f1a\u53bb\u5f15\u7528\u7684\uff0c\u56e0\u4e3a\u9884\u7f16\u8bd1\u4e4b\u540e\u7684\u4ee3\u7801\u903b\u8f91\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre>var foo = 3;\r\n\r\n\/\/ \u9884\u7f16\u8bd1\u4e4b\u540e\r\nfunction hoistVariable() {\r\n    var foo;\r\n\r\n    foo = foo || 5;\r\n\r\n    console.log(foo); \/\/ 5\r\n}\r\n\r\nhoistVariable();<\/pre>\n<p>\u5982\u679c\u5f53\u524d\u4f5c\u7528\u57df\u4e2d\u58f0\u660e\u4e86\u591a\u4e2a\u540c\u540d\u53d8\u91cf\uff0c\u90a3\u4e48\u6839\u636e\u6211\u4eec\u7684\u63a8\u65ad\uff0c\u5b83\u4eec\u7684\u540c\u4e00\u4e2a\u6807\u8bc6\u7b26\u4f1a\u88ab\u63d0\u5347\u81f3\u4f5c\u7528\u57df\u9876\u90e8\uff0c\u5176\u4ed6\u90e8\u5206\u6309\u987a\u5e8f\u6267\u884c\uff0c\u6bd4\u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>function hoistVariable() {\r\n    var foo = 3;\r\n    \r\n    {\r\n        var foo = 5;\r\n    }\r\n\r\n    console.log(foo); \/\/ 5\r\n}\r\n\r\nhoistVariable();<\/pre>\n<\/div>\n<p>\u7531\u4e8eJavaScript\u6ca1\u6709\u5757\u4f5c\u7528\u57df\uff0c\u53ea\u6709\u5168\u5c40\u4f5c\u7528\u57df\u548c\u51fd\u6570\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u9884\u7f16\u8bd1\u4e4b\u540e\u7684\u4ee3\u7801\u903b\u8f91\u4e3a\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>\/\/ \u9884\u7f16\u8bd1\u4e4b\u540e\r\nfunction hoistVariable() {\r\n    var foo;\r\n\r\n    foo = 3;\r\n    \r\n    {\r\n        foo = 5;\r\n    }\r\n\r\n    console.log(foo); \/\/ 5\r\n}\r\n\r\nhoistVariable();<\/pre>\n<h1 class=\"cnblogs_code_toolbar\">\u51fd\u6570\u63d0\u5347<\/h1>\n<div class=\"cnblogs_code\">\n<pre>function hoistFunction() {\r\n    foo(); \/\/ output: I am hoisted\r\n\r\n    function foo() {\r\n        console.log('I am hoisted');\r\n    }\r\n}\r\n\r\nhoistFunction();<\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>\u4e3a\u4ec0\u4e48\u51fd\u6570\u53ef\u4ee5\u5728\u58f0\u660e\u4e4b\u524d\u5c31\u53ef\u4ee5\u8c03\u7528\uff0c\u5e76\u4e14\u8ddf\u53d8\u91cf\u58f0\u660e\u4e0d\u540c\u7684\u662f\uff0c\u5b83\u8fd8\u80fd\u5f97\u5230\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u5176\u5b9e\u5f15\u64ce\u662f\u628a\u51fd\u6570\u58f0\u660e\u6574\u4e2a\u5730\u63d0\u5347\u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df\u7684\u9876\u90e8\uff0c\u9884\u7f16\u8bd1\u4e4b\u540e\u7684\u4ee3\u7801\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>\/\/ \u9884\u7f16\u8bd1\u4e4b\u540e\r\nfunction hoistFunction() {\r\n    function foo() {\r\n        console.log('I am hoisted');\r\n    }\r\n\r\n    foo(); \/\/ output: I am hoisted\r\n}\r\n\r\nhoistFunction();<\/pre>\n<div class=\"cnblogs_code_toolbar\">\u5728\u5f00\u59cb\u4e4b\u524d\u4f1a\u8fdb\u884c\u51fd\u6570\u9884\u89e3\u91ca\uff0c\u5148\u786e\u8ba4\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e0b\u6709\u4e00\u4e2a \u53eb\u505ahoistfunction\u7684\u51fd\u6570\uff0c\u7136\u540e\u89e3\u6790\u5b9a\u4e49\u6709\u4e2afoo\u7684\u51fd\u6570\uff0c\u7b49\u5230\u51fd\u6570\u7f16\u8bd1\u7684\u65f6\u5019 \uff0c\u4f1a\u628a\u51fd\u6570\u58f0\u660e\u63d0\u5347\u5230\u9876\u90e8\uff0c\u8fd9\u4e5f\u5c31\u662f\u4e3a\u4ec0\u4e48\u6253\u5370\u51fa\u6765\u662f\u8fd9\u4e2a\u6837\u5b50\u3002<\/div>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>\u76f8\u4f3c\u7684\uff0c\u5982\u679c\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\u5b58\u5728\u591a\u4e2a\u540c\u540d\u51fd\u6570\u58f0\u660e\uff0c\u540e\u9762\u51fa\u73b0\u7684\u5c06\u4f1a\u8986\u76d6\u524d\u9762\u7684\u51fd\u6570\u58f0\u660e<\/p>\n<pre>function hoistFunction() {\r\n    function foo() {\r\n        console.log(1);\r\n    }\r\n\r\n    foo(); \/\/ output: 2\r\n\r\n    function foo() {\r\n        console.log(2);\r\n    }\r\n}\r\n\r\nhoistFunction();<\/pre>\n<div class=\"cnblogs_code_toolbar\">\n<p>\u9664\u4e86\u4f7f\u7528\u4e0a\u9762\u7684\u51fd\u6570\u58f0\u660e\uff0c\u66f4\u591a\u65f6\u5019\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u4e0b\u9762\u662f\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u5bf9\u6bd4\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>\/\/ \u51fd\u6570\u58f0\u660e\r\nfunction foo() {\r\n    console.log('function declaration');\r\n}\r\n\r\n\/\/ \u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\r\nvar foo = function() {\r\n    console.log('anonymous function expression');\r\n};\r\n\r\n\/\/ \u5177\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\r\nvar foo = function bar() {\r\n    console.log('named function expression');\r\n};<\/pre>\n<\/div>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u5176\u5b9e\u662f\u5c06\u4e00\u4e2a\u4e0d\u5e26\u540d\u5b57\u7684\u51fd\u6570\u58f0\u660e\u8d4b\u503c\u7ed9\u4e86\u4e00\u4e2a\u53d8\u91cf\uff0c\u800c\u5177\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u5219\u662f\u5e26\u540d\u5b57\u7684\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u9700\u8981\u6ce8\u610f\u5230\u662f\uff0c\u8fd9\u4e2a\u51fd\u6570\u540d\u53ea\u80fd\u5728\u6b64\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002\u6211\u4eec\u4e5f\u770b\u5230\u4e86\uff0c\u5176\u5b9e\u51fd\u6570\u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u8bbf\u95ee\uff0c\u6240\u4ee5\u4e5f\u5b58\u5728\u53d8\u91cf\u63d0\u5347\u540c\u6837\u7684\u6548\u679c\u3002<\/p>\n<p>\u90a3\u4e48\u5f53\u51fd\u6570\u58f0\u660e\u9047\u5230\u51fd\u6570\u8868\u8fbe\u5f0f\u65f6\uff0c\u4f1a\u6709\u4ec0\u4e48\u6837\u7684\u7ed3\u679c\u5462\uff0c\u5148\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>function hoistFunction() {\r\n    foo(); \/\/ 2\r\n\r\n    var foo = function() {\r\n        console.log(1);\r\n    };\r\n\r\n    foo(); \/\/ 1\r\n\r\n    function foo() {\r\n        console.log(2);\r\n    }\r\n\r\n    foo(); \/\/ 1\r\n}\r\n\r\nhoistFunction();<\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>\u8fd0\u884c\u540e\u6211\u4eec\u4f1a\u53d1\u73b0\uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u4f9d\u6b21\u662f2 1 1\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u6709\u8fd9\u6837\u7684\u7ed3\u679c\u5462\uff1f<\/p>\n<p>\u56e0\u4e3aJavaScript\u4e2d\u7684\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\uff0c<span style=\"color: #993300;\">\u51fd\u6570\u58f0\u660e\u7684\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u4f1a\u88ab\u63d0\u5347\u81f3\u5f53\u524d\u4f5c\u7528\u57df\u6700\u9876\u7aef\uff0c<\/span>\u6240\u4ee5\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\u5b9e\u9645\u6267\u884c\u4e86\u4e0b\u9762\u5b9a\u4e49\u7684\u51fd\u6570\u58f0\u660e\uff0c\u7136\u540e\u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff0c\u7531\u4e8e\u524d\u9762\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\u4e0e\u4e4b\u524d\u7684\u51fd\u6570\u58f0\u660e\u540c\u540d\uff0c\u6545\u5c06\u5176\u8986\u76d6\uff0c\u4ee5\u540e\u7684\u8c03\u7528\u4e5f\u5c06\u4f1a\u6253\u5370\u540c\u6837\u7684\u7ed3\u679c\u3002\u4e0a\u9762\u7684\u8fc7\u7a0b\u7ecf\u8fc7\u9884\u7f16\u8bd1\u4e4b\u540e\uff0c\u4ee3\u7801\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>\/\/ \u9884\u7f16\u8bd1\u4e4b\u540e\r\nfunction hoistFunction() {\r\n    var foo;\r\n\r\n    foo = function foo() {\r\n        console.log(2);\r\n    }\r\n\r\n    foo(); \/\/ 2\r\n\r\n    foo = function() {\r\n        console.log(1);\r\n    };\r\n\r\n    foo(); \/\/ 1\r\n\r\n    foo(); \/\/ 1\r\n}\r\n\r\nhoistFunction();<\/pre>\n<\/div>\n<p>\u6211\u4eec\u4e5f\u4e0d\u96be\u7406\u89e3\u4e0b\u9762\u7684\u51fd\u6570\u548c\u53d8\u91cf\u91cd\u540d\u65f6\uff0c\u4f1a\u5982\u4f55\u6267\u884c\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>var foo = 3;\r\n\r\nfunction hoistFunction() {\r\n    console.log(foo); \/\/ function foo() {}\r\n\r\n    foo = 5;\r\n    \r\n    console.log(foo); \/\/ 5\r\n\r\n    function foo() {}\r\n}\r\n\r\nhoistFunction();\r\nconsole.log(foo);     \/\/ 3<\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u51fd\u6570\u58f0\u660e\u88ab\u63d0\u5347\u81f3\u4f5c\u7528\u57df\u6700\u9876\u7aef\uff0c\u7136\u540e\u88ab\u8d4b\u503c\u4e3a5\uff0c\u800c\u5916\u5c42\u7684\u53d8\u91cf\u5e76\u6ca1\u6709\u88ab\u8986\u76d6\uff0c\u7ecf\u8fc7\u9884\u7f16\u8bd1\u4e4b\u540e\uff0c\u4e0a\u9762\u4ee3\u7801\u7684\u903b\u8f91\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>\/\/ \u9884\u7f16\u8bd1\u4e4b\u540e\r\n\r\nvar foo = 3;\r\n\r\nfunction hoistFunction() {\r\n   var foo;\r\n\r\n   foo = function foo() {};\r\n\r\n   console.log(foo); \/\/ function foo() {}\r\n   \r\n   foo = 5;\r\n\r\n   console.log(foo); \/\/ 5\r\n}\r\n\r\nhoistFunction();\r\nconsole.log(foo);    \/\/ 3<\/pre>\n<\/div>\n<p>\u6240\u4ee5\uff0c\u51fd\u6570\u7684\u4f18\u5148\u6743\u662f\u6700\u9ad8\u7684\uff0c\u5b83\u6c38\u8fdc\u88ab\u63d0\u5347\u81f3\u4f5c\u7528\u57df\u6700\u9876\u90e8\uff0c\u7136\u540e\u624d\u662f\u51fd\u6570\u8868\u8fbe\u5f0f\u548c\u53d8\u91cf\u6309\u987a\u5e8f\u6267\u884c\uff0c\u8fd9\u4e00\u70b9\u8981\u7262\u8bb0\u3002<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u53d8\u91cf\u63d0\u5347 \u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u51fd\u6570\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cf\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u58f0\u660e\u662f\u5728if\u8bed\u53e5\u5757\u4e2d\uff1a function ho [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/624"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=624"}],"version-history":[{"count":1,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/624\/revisions"}],"predecessor-version":[{"id":625,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/624\/revisions\/625"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}