René's URL Explorer Experiment


Title: 作用域闭包 · Issue #30 · paddingme/Learning-JavaScript · GitHub

Open Graph Title: 作用域闭包 · Issue #30 · paddingme/Learning-JavaScript

X Title: 作用域闭包 · Issue #30 · paddingme/Learning-JavaScript

Description: 看起来像闭包。 function foo(){ var a = 2; function bar(){ console.log(a); } bar(); } foo(); 这才是闭包。 function foo(){ var a = 2; function bar(){ console.log(a); } return bar; } var baz = foo(); baz(); 把内部函数 baz 传递给 bar, 当调用这个内部函数时(现在叫做 fn),它涵盖的 fo...

Open Graph Description: 看起来像闭包。 function foo(){ var a = 2; function bar(){ console.log(a); } bar(); } foo(); 这才是闭包。 function foo(){ var a = 2; function bar(){ console.log(a); } return bar; } var baz = foo(); baz(); 把内部函数 ...

X Description: 看起来像闭包。 function foo(){ var a = 2; function bar(){ console.log(a); } bar(); } foo(); 这才是闭包。 function foo(){ var a = 2; function bar(){ console.log(a); } return bar; } var baz = foo(); baz(); 把内部函数 ...

Opengraph URL: https://github.com/paddingme/Learning-JavaScript/issues/30

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"作用域闭包","articleBody":"1. 看起来像闭包。\n   \n   ``` js\n   function foo(){\n       var a = 2;\n       function bar(){\n           console.log(a);\n       }\n       bar();\n   }\n   foo();\n   ```\n2. 这才是闭包。\n   \n   ``` js\n   function foo(){\n       var a = 2;\n       function bar(){\n           console.log(a);\n       }\n       return bar;\n   }\n   var baz = foo();\n   baz();\n   ```\n3. 把内部函数 baz 传递给 bar, 当调用这个内部函数时(现在叫做 fn),它涵盖的 foo()\n   内部作用域的闭包就可以观察到了。因此它能够访问 a。\n   \n   ``` js\n   function foo() {\n       var a = 2;\n       function baz(){\n           console.log(a);\n       }\n       bar(baz);\n   }\n   function bar(fn) {\n       fn();\n   }\n   ```\n4. 把 baz 分配给全局变量\n   \n   ``` js\n   var fn;\n   function foo(){\n       var a = 2;\n       function baz(){\n           console.log(a);\n       }\n       fn = baz;\n   }\n   function bar(){\n       fn();\n   }\n   foo();\n   bar();\n   ```\n5. 无论通过何种手段将内部函数传递到所在的词法作用域以外, \n   它都会持有对原始定义作用有的引用, \n   无论在何处执行这个函数都会使用闭包。\n   \n   ``` js\n   function wait(message) {\n       setTimeout(function timer(){\n           console.log(message);\n       },1000)\n   }\n   wait(\"Hello,Closure!\")\n   ```\n   \n   /*\n   jquery\n   */\n   \n   ``` js\n   function setupBot(name,selector){\n       $(selector).click(function activator(){\n           console.log(\"Activating: \" + name);\n       });\n   }\n   setupBot(\"console BOT1\",\"#bot_1\");\n   setupBot(\"console BOT2\",\"#bot_2\");\n   ```\n6. IIFE\n   \n   ``` js\n   var a = 2;\n   (function IIFE(){\n       console.log(a);\n   })();\n   ```\n7. 循环和闭包\n   \n   ``` js\n   for(var i = 1; i \u003c= 5; i++) {\n       setTimeout(function timer(){\n           console.log(i)\n       },i*1000)\n   }\n   \n   \n   for(var i = 0; i\u003c=5; i++) {\n       (function(j){\n           setInterval(function(){\n               console.log(j);\n           },1000);\n       })(i);\n   }\n   ```\n   \n   ``` js\n   // 每隔一秒钟打印1,2,3,4,5\n   function foo(arr){\n       var i = 0;\n       return function(){\n           setInterval(function(){\n               console.log(arr[i++]);\n           },1000);\n       }()\n   }\n   ```\n   \n   ``` js\n   (function(arr){\n       var i=0,length=arr.length;\n       (function a(){\n           setTimeout(function(){\n               console.log(arr[i++]);\n               i\u003clength\u0026\u0026a();\n           },1000)\n       }())\n   }([1,2,3,4]))\n   ```\n8.  模块\n   \n   ``` js\n   function coolModule() {\n        var sth = \"cool\";\n        var another = [1,2,3];\n        function doSth() {\n           console.log(sth);\n        }\n        function doAnother() {\n           console.log(another.join(\"!\"));\n        }\n        return {\n           doSth:  doSth,\n           doAnother : doAnother\n        }\n   }\n   var foo = new coolModule();\n   foo.doSth();\n   foo.doAnother();\n   ```\n9. 模块模式需要两个必要条件:\n   1. 必须有外部的封闭函数,该函数必须至少\n      被调用一次(每次调用都会创建一个新的模块实例)\n   2. 封闭函数必须至少返回至少一个内部函数,\n      这样内部函数才能在私有作用域中形成闭包,\n      并且可以访问或者修改私有的状态。\n10.  模块模式另一个简单强大的变化用法是,命名将要作为公共API 返回的对象。\n    \n    ``` js\n    var foo = (function coolModule(id){\n        function change(){\n            publicAPI.identify = identify2;\n        }\n        function identify1(){\n            console.log(id);\n        }\n        function identify2(){\n            console.log(id.toUpperCase());\n        }\n        var publicAPI = {\n            change:change,\n            identify: identify1\n        };\n        return publicAPI;\n    })(\"foo module\");\n    foo.identify();\n    foo.change();\n    foo.identify();\n    ```\n11.  现代的模块管理\n    \n    ``` js\n    var MyModules = (function Manger() {\n        var modules = {};\n        function define(name, deps, impl) {\n            for (var i = 0; i \u003c deps.length; i++) {\n                deps[i] = modules[deps[i]];\n            }\n            modules[name] = impl.apply(impl, deps);\n        }\n        function get(name) {\n            return modules[name];\n        }\n        return {\n            define: define,\n            get: get\n        }\n    })();\n    \n    MyModules.define(\"bar\", [], function() {\n        function hello(who) {\n            return \"Let me introduce: \" + who;\n        }\n        return {\n            hello: hello\n        };\n    });\n    MyModules.define(\"foo\", [\"bar\"], function(bar) {\n        var hungry = \"hippo\";\n        function awesome() {\n            console.log(bar.hello(hungry).toUpperCase());\n        }\n        return {\n            awesome: awesome\n        };\n    });\n    var bar = MyModules.get(\"bar\");\n    var foo = MyModules.get(\"foo\");\n    console.log(bar.hello(\"hippo\"));\n    foo.awesome();\n    ```\n\n当函数可以记住并访问所在的词法作用域,即使函数时在当前词法作用域之外执行,这时就产生了闭包。\n\n模块有两个主要特征: \n1. 为创建内部作用域而调用了一个包装函数;\n2. 包装函数的返回值必须至少包含一个队内部函数的引用,这样就会创建涵盖整个包装函数内部作用域的闭包。\n","author":{"url":"https://github.com/paddingme","@type":"Person","name":"paddingme"},"datePublished":"2015-08-22T13:50:56.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/30/Learning-JavaScript/issues/30"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:9db64c1f-b9bb-447b-761f-08a19a0d5d04
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9A46:225DC1:A207613:DA5242D:6A5EA957
html-safe-nonce668348fe75e9051f1da3d77973643182162aa7b3d51b71831e391adf964c2609
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTQ2OjIyNURDMTpBMjA3NjEzOkRBNTI0MkQ6NkE1RUE5NTciLCJ2aXNpdG9yX2lkIjoiODA0NDc2NTEzOTI0ODc4NTc1MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac6bce0053b0d42169ad9cf9d615d9f84933040529cc317ba377353671ed6b8270
hovercard-subject-tagissue:102538266
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/paddingme/Learning-JavaScript/30/issue_layout
twitter:imagehttps://opengraph.githubassets.com/7b706d980df36e9277e21e8a9da7820808d8c83aa7b2f144b1f0791ce5e690eb/paddingme/Learning-JavaScript/issues/30
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/7b706d980df36e9277e21e8a9da7820808d8c83aa7b2f144b1f0791ce5e690eb/paddingme/Learning-JavaScript/issues/30
og:image:alt看起来像闭包。 function foo(){ var a = 2; function bar(){ console.log(a); } bar(); } foo(); 这才是闭包。 function foo(){ var a = 2; function bar(){ console.log(a); } return bar; } var baz = foo(); baz(); 把内部函数 ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamepaddingme
hostnamegithub.com
expected-hostnamegithub.com
None7c7e31acb6a895494e518b880f5ccf39604f7fa9a8f2f3c64145efc3b776256d
turbo-cache-controlno-preview
go-importgithub.com/paddingme/Learning-JavaScript git https://github.com/paddingme/Learning-JavaScript.git
octolytics-dimension-user_id5771087
octolytics-dimension-user_loginpaddingme
octolytics-dimension-repository_id25473884
octolytics-dimension-repository_nwopaddingme/Learning-JavaScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id25473884
octolytics-dimension-repository_network_root_nwopaddingme/Learning-JavaScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release2d2ac9bdd71d5f53f2b731c9330677e38624e301
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/paddingme/Learning-JavaScript/issues/30#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpaddingme%2FLearning-JavaScript%2Fissues%2F30
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpaddingme%2FLearning-JavaScript%2Fissues%2F30
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=paddingme%2FLearning-JavaScript
Reloadhttps://github.com/paddingme/Learning-JavaScript/issues/30
Reloadhttps://github.com/paddingme/Learning-JavaScript/issues/30
Reloadhttps://github.com/paddingme/Learning-JavaScript/issues/30
paddingme https://github.com/paddingme
Learning-JavaScripthttps://github.com/paddingme/Learning-JavaScript
Notifications https://github.com/login?return_to=%2Fpaddingme%2FLearning-JavaScript
Fork 13 https://github.com/login?return_to=%2Fpaddingme%2FLearning-JavaScript
Star 28 https://github.com/login?return_to=%2Fpaddingme%2FLearning-JavaScript
Code https://github.com/paddingme/Learning-JavaScript
Issues 30 https://github.com/paddingme/Learning-JavaScript/issues
Pull requests 0 https://github.com/paddingme/Learning-JavaScript/pulls
Actions https://github.com/paddingme/Learning-JavaScript/actions
Projects https://github.com/paddingme/Learning-JavaScript/projects
Wiki https://github.com/paddingme/Learning-JavaScript/wiki
Security and quality 0 https://github.com/paddingme/Learning-JavaScript/security
Insights https://github.com/paddingme/Learning-JavaScript/pulse
Code https://github.com/paddingme/Learning-JavaScript
Issues https://github.com/paddingme/Learning-JavaScript/issues
Pull requests https://github.com/paddingme/Learning-JavaScript/pulls
Actions https://github.com/paddingme/Learning-JavaScript/actions
Projects https://github.com/paddingme/Learning-JavaScript/projects
Wiki https://github.com/paddingme/Learning-JavaScript/wiki
Security and quality https://github.com/paddingme/Learning-JavaScript/security
Insights https://github.com/paddingme/Learning-JavaScript/pulse
作用域闭包https://github.com/paddingme/Learning-JavaScript/issues/30#top
https://github.com/paddingme
paddingmehttps://github.com/paddingme
on Aug 22, 2015https://github.com/paddingme/Learning-JavaScript/issues/30#issue-102538266
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.