JavaScript Patterns 4.5 Immediate Functions
The immediate function pattern is a syntax that enables you to execute a function as soon as it is defined.
(function () { alert('watch out!'); }());
• You define a function using a function expression. (A function declaration won’t work.)
• You add a set of parentheses at the end, which causes the function to be executed immediately.
• You wrap the whole function in parentheses (required only if you don’t assign the function to a variable).
(function () { var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], today = new Date(), msg = 'Today is ' + days[today.getDay()] + ', ' + today.getDate(); alert(msg); }()); // "Today is Fri, 13"
If this code weren’t wrapped in an immediate function, then the variables days, today, and msg would all be global variables, leftovers from the initialization code.
Parameters of an Immediate Function
// prints: // I met Joe Black on Fri Aug 13 2010 23:26:59 GMT-0800 (PST) (function (who, when) { console.log("I met " + who + " on " + when); }("Joe Black", new Date()));
Commonly, the global object is passed as an argument to the immediate function so that it’s accessible inside of the function without having to use window.
(function (global) { // access the global object via `global` }(this));
Shouldn’t pass too many parameters to an immediate function, because it could quickly become a burden to constantly scroll to the top and to the bottom of the function to understand how it works.
Returned Values from Immediate Functions
var result = (function () { return 2 + 2; }());
use the scope of the immediate function to privately store some data, specific to the inner function you return.
var getResult = (function () { var res = 2 + 2; return function () { return res; }; }());
Used for Defining object properties
var o = { message: (function () { var who = "me", what = "call"; return what + " " + who; }()), getMsg: function () { return this.message; } }; // usage o.getMsg(); // "call me" o.message; // "call me"
Benefits and Usage
It helps you wrap an amount of work you want to do without leaving any global variables behind. All the variables you define will be local to the self-invoking functions and you don’t have to worry about polluting the global space with temporary variables.
Enables you to wrap individual features into self-contained modules.
// module1 defined in module1.js (function () { // all the module 1 code ... }());
JavaScript Patterns 4.5 Immediate Functions的更多相关文章
- JavaScript Patterns 4.4 Self-Defining Functions
If you create a new function and assign it to the same variable that already holds another function, ...
- JavaScript Patterns 4.3 Returning Functions
Use closure to store some private data, which is accessible by the returned function but not to the ...
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 5.8 Chaining Pattern
Chaining Pattern - Call methods on an object one after the other without assigning the return values ...
- JavaScript Patterns 5.5 Sandbox Pattern
Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- JavaScript Patterns 4.10 Curry
Function Application apply() takes two parameters: the first one is an object to bind to this inside ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
随机推荐
- Cocos2d-x SpriteFrameCache的使用
根据官方文档与其他c++属性实现的类似书中示例: 图中的小人是会动的. 首先使用texturepacker制作出一张拼图,导出plist等文件: plist类似: 3.x的cocos似乎不再鼓励使用什 ...
- Mysql学习笔记(十三)权限管理
学习内容: 1.权限管理: 关于mysql的权限简单的理解就是mysql允许你做你权利以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执行update操作.只允许你从某台机器上连 ...
- 【第〇课】用更好的IDE开发
推荐的IDE IntelliJ IDEA JetBrains是一家捷克的软件开发公司,这家公司做出了很多的优秀的IDE,有写python的.php的.web前端的……IDEA就是其中一款非常优秀的ja ...
- 通过Nginx实现负载均衡
百度了下负载均衡:英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器.FTP服务器.企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务 嗯,跟我理 ...
- Mybatis choose (when, otherwise)标签
choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束.当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的s ...
- 移动端前端常见的触摸相关事件touch、tap、swipe等整理
前端的很多事件在PC端和浏览器端可公用,但有些事件却只在移动端产生,如触摸相关的事件 本文整理了移动端常见的一些事件,包括原生支持的click.touch.tap.swipe事件,也有定义型的gest ...
- Cookie 和 Session 的区别
[[ from 老生常谈session,cookie的区别,安全性 ]] 一,为什么session,cookie经常会有人提到 做web开发的人基本上都会用session和cookie,但是仅仅只是会 ...
- 树莓派配置静态ip
#vim /etc/network/interfaces 修改为如下内容: auto eth0 iface eth0 inet static address 192.168.0.2 netmask 2 ...
- 【原创随笔】Sql2008 R2 做CQRS小结
1.做数据同步,订阅服务器只要把数据库建好就可以了,至于表和存储过程以及其它的都不管,订阅的时候会自动创建这些信息. 2.选择事务发布(如果同步表,表至少要带主键,不然不能选择) 3.在发布的时候,用 ...
- 在SQL存储过程中给条件变量加上单引号
在SQL存储过程中给条件变量加上单引号,不加语句就会出问题,以下就是在存储过程中将条件where设置成了动态变化的,给where赋完值再和前面的语句拼接,再execute(SQL) ), )), )+ ...