[翻译]High Performance JavaScript(014)
Recursion Patterns 递归模式
When you run into a call stack size limit, your first step should be to identify any instances of recursion in the code. To that end, there are two recursive patterns to be aware of. The first is the straightforward recursive pattern represented in the factorial() function shown earlier, when a function calls itself. The general pattern is as follows:
当你陷入调用栈尺寸限制时,第一步应该定位在代码中的递归实例上。为此,有两个递归模式值得注意。首先是直接递归模式为代表的前面提到的factorial()函数,即一个函数调用自身。其一般模式如下:
function recurse(){
recurse();
}
recurse();
This pattern is typically easy to identify when errors occur. A second, subtler pattern involves two functions:
当发生错误时,这种模式比较容易定位。另外一种模式称为精巧模式,它包含两个函数:
function first(){
second();
}
function second(){
first();
}
first();
In this recursion pattern, two functions each call the other, such that an infinite loop is formed. This is the more troubling pattern and a far more difficult one to identify in large code bases.
在这种递归模式中,两个函数互相调用对方,形成一个无限循环。这是一个令人不安的模式,在大型代码库中定位错误很困难。
Most call stack errors are related to one of these two recursion patterns. A frequent cause of stack overflow is an incorrect terminal condition, so the first step after identifying the pattern is to validate the terminal condition. If the terminal condition is correct, then the algorithm contains too much recursion to safely be run in the browser and should be changed to use iteration, memoization, or both.
大多数调用栈错误与这两种模式之一有关。常见的栈溢出原因是一个不正确的终止条件,所以定位模式错误的第一步是验证终止条件。如果终止条件是正确的,那么算法包含了太多层递归,为了能够安全地在浏览器中运行,应当改用迭代,制
相关文档:
var
xmlDoc
=
null
;
function
parseXML
(
xmlUrl
)
{
try
{
//IE
xmlDoc
=
new
ActiveXObject
(
"Microsoft.XMLDOM"
);
xmlDoc
.
async
=
false
;
xmlDoc
......
Javascript数据类型
由于javascript是弱类型语言,即定义变量时不必声明其类型
。但这并不意味着变量没有类型。因为赋值时会自动匹配数据类型!
目前用到的基本数据类型
number
boolean
string
var i
i="test";//这时i就成了string类型
var i
i=4;//这时i就成了number类型
常用对象类型<object> ......
Closure中文翻译为闭包.字面上来理解就是"封闭的包".(这是一句废话)
闭包是什么?
书面解释为:
所谓“闭包”,指的是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分。
我认为闭包就是能够读/写函数内部的某些变量的子函数,并将这些变量保存 ......