Created
April 4, 2012 08:29
-
-
Save haohello/2299694 to your computer and use it in GitHub Desktop.
创建匿名函数并将其赋值于一个变量
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 标题: 函数声明 | |
* 描述: 创建匿名函数并将该函数赋值于一个变量 | |
*/ | |
// 反模式 | |
function getData() { | |
} | |
// 建议的模式 | |
/* 好处: | |
* 1. 更易理解"函数是一个对象". | |
* 2. 强化了分号结尾的好习惯. | |
* 3. 去掉了传统中附加于函数和变量范围的包袱. | |
*/ | |
var getData = function () { | |
}; | |
// 命名的函数写法 | |
/* 好处: | |
* 1. 为编译工具提供了显式的函数名称: 以便于堆栈跟踪调试. | |
* 2. 允许递归函数: getData函数可以调用它本身. | |
* 问题: | |
* 1. 在老的IE中会有些奇怪的现象 | |
*/ | |
var getData = function getData () { | |
}; | |
// 参考 | |
// http://ejohn.org/blog/javascript-as-a-first-language/ | |
// http://kangax.github.com/nfe/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment