[翻译]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.
大多数调用栈错误与这两种模式之一有关。常见的栈溢出原因是一个不正确的终止条件,所以定位模式错误的第一步是验证终止条件。如果终止条件是正确的,那么算法包含了太多层递归,为了能够安全地在浏览器中运行,应当改用迭代,制
相关文档:
页面提交数据一般有两种方法:get,post。post就是所谓的form提交,使用视图;get是通过url提交。
Get方法一般用后台代码(如asp,asp.net)获得参数,代码很简单:Request.QueryString["id"];即可获取。
有些时候需要直接在前台获取url参数,要用到javascript,js没有直接获取url参数的方法,那么,我们如何通过js ......
<script>
//写cookies函数 作者:翟振凯
function
SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
{
var Days = 30;
//此 cookie 将被保存 30 天
var exp = new Date(); //new
Date("December 31, 9998");
  ......
3、组合构造函数/原型方式写类,采用前面种方式继承
这种方式父类,子类的属性都挂在构造函数里,方法都挂在原型上。
/**
* 父类Polygon:多边形
*/
function Polygon(sides) {
this.sides = sides;
}
Polygon.prototype.setSides = function(s) {this.sides=s;}
/**
* Triangle 三角形
* @param {Object} b ......
Javascript数据类型
由于javascript是弱类型语言,即定义变量时不必声明其类型
。但这并不意味着变量没有类型。因为赋值时会自动匹配数据类型!
目前用到的基本数据类型
number
boolean
string
var i
i="test";//这时i就成了string类型
var i
i=4;//这时i就成了number类型
常用对象类型<object> ......