博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript Patterns 4.5 Immediate Functions
阅读量:4987 次
发布时间:2019-06-12

本文共 2557 字,大约阅读时间需要 8 分钟。

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;    }};// usageo.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 ...}());

转载于:https://www.cnblogs.com/haokaibo/p/Immediate-Functions.html

你可能感兴趣的文章
【分享】如何设计更加“面向对象”的三层架构系统(1)
查看>>
实验五总结
查看>>
C++回调函数
查看>>
Phpstorm-Xdebug配置
查看>>
C#总结项目《影院售票系统》编写总结三
查看>>
Linux中命令行编译java接口总是提示找不到符号的疑难杂症的解决
查看>>
WF中创建持久化服务和跟踪服务数据库
查看>>
微软企业库5.0系统(一):使用缓存 Microsoft.Practices.EnterpriseLibrary.Caching(初级篇)...
查看>>
5.29
查看>>
浅谈Java中的equals和==(转载)
查看>>
性能测试之稳定性测试(可靠性测试)
查看>>
Flask02 路由的书写、蓝图、利用蓝图实现url前缀、利用蓝图实现子域名、访问静态文件...
查看>>
linux c lseek (空洞文件) 分析和处理
查看>>
String分析
查看>>
MySQL学习——SQL查询语句(连接查询&子查询)(三)
查看>>
oracle pl sql 行转列 (数据翻转实现)
查看>>
优秀的项目经理需要具备哪些品质?
查看>>
Avi视频生成缩略图时,提示“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”...
查看>>
命令行执行python模块时提示ImportError: No module named xxx
查看>>
WPF界面假死
查看>>