技术控

    今日:41| 主题:49369
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Using Anonymous and Self-Executing Functions in JavaScript

[复制链接]
欧豪呕霸 发表于 2016-10-1 16:47:10
100 1

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
While    functions were introduced in this reading list seriesas    namedtasks, it’s also possible to create    anonymousfunctions, which have several important roles inJavaScript.  
  Everyday Anonymity

  You’re probably already encountered anonymous functions: they’re an intrinsic part ofcallbacks.  
  1. document.getElementById("button").addEventListener("click",
  2.     function (){
  3.         …
  4.     })
复制代码
Because the function used in theevent listener is not named, it’s    anonymous. Anonymous functions are frequently    created and run at the same moment,as they are in a callback.  
  An anonymous function can also be the value of avariable, as a function    expression:  
  1. var luke = function() {
  2.     // use the force
  3. }
复制代码
In this way, the function can be referred to later by the name of the    variable,although such a function is still considered anonymous.  
  Distinctions with a Difference

  One important distinction about anonymous functions is that they    mustbe defined before making any reference to them. You might recall    back in the first articlein this series that traditional functions can be declared anywhere in your code and referenced from anywhere, since the browser will parse your code before executing it, picking up any named functions along the way; it’s only    convention(and good coding practice) that functions are written at the start of your script. But because an anonymous function is    created at the moment it is run in the script, not when the script is parsed, it can’t be written    aftera reference to it in your code.  
  Uses for Anonymous Functions

  Because they don’t need a name, anonymous functions are shorter and easier to write if you don’t need a reference to them elsewhere; that’s one of the reasons they’re used in callbacks. There’s also one interesting feature of anonymous functions: used in a particular way, they can    run themselves.  
  1. (function() {
  2.     // run a bunch of code automatically
  3. })();
复制代码
The technique, known as a self-executing or    self-invokedfunction, may seem to be somewhat redundant: ordinary JavaScript will also be executed when it is encountered in your code, so why use this odd-looking function?  
  The primary reason is to take advantage of          scope    . You may recall that variables declared    insidefunctions    can only be referenced inside the context of that function;they’re not recognized at all outside of it. As such, creating variables inside a self-executing function “locks off” the function, and prevents the variables from being accidentally referenced or overwritten elsewhere in your code. The technique neatly encapsulates the variables and code, keeping them out of the    global namespaceand ensuring they don’t clash with other code written elsewhere. As such, additions to code such aspolyfills or plugins are frequently written as self-executing functions.  
  Conclusion

  Understanding anonymous and self-executing functions allows you to pass functions around as variables and modularize your code. But functions can be used for much more: they can even create and return other functions. That’s the role of closures, which we will look at next.
  Photograph by    Smilla, used under a    Creative Commons Attribution-NonCommercial 2.0 Generic license  
  Enjoy this piece? I invite you to follow me at    twitter.com/dudleystoreyto learn more.
友荐云推荐




上一篇:#GTMTips: Debug Change History In GTM
下一篇:Keeping our design team sharp with lots of learning
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

bj7ldisease 发表于 2016-10-4 04:26:24
帖子很有深度!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表