[翻译]High Performance JavaScript(021)
Splitting Up Tasks 分解任务
What we typically think of as one task can often be broken down into a series of subtasks. If a single function is taking too long to execute, check to see whether it can be broken down into a series of smaller functions that complete in smaller amounts of time. This is often as simple as considering a single line of code as an atomic task, even though multiple lines of code typically can be grouped together into a single task. Some functions are already easily broken down based on the other functions they call. For example:
我们通常将一个任务分解成一系列子任务。如果一个函数运行时间太长,那么查看它是否可以分解成一系列能够短时间完成的较小的函数。可将一行代码简单地看作一个原子任务,多行代码组合在一起构成一个独立任务。某些函数可基于函数调用进行拆分。例如:
function saveDocument(id){
//save the document
openDocument(id)
writeText(id);
closeDocument(id);
//update the UI to indicate success
updateUI(id);
}
If this function is taking too long, it can easily be split up into a series of smaller steps by breaking out the individual methods into separate timers. You can accomplish this by adding each function into an array and then using a pattern similar to the array-processing pattern from the previous section:
如果函数运行时间太长,它可以拆分成一系列更小的步骤,把独立方法放在定时器中调用。你可以将每个函数都放入一个数组,然后使用前一节中提到的数组处理模式:
function saveDocument(id){
var tasks = [openDocument, writeText, closeDocument, updateUI];
setTimeout(function(){
//execute the next task
var task = tasks.shift();
task(id);
//determine if there's more
if (tasks.length > 0){
setTimeout(arguments.callee, 25);
}
}, 25);
}
 
相关文档:
Javascript数据类型
由于javascript是弱类型语言,即定义变量时不必声明其类型
。但这并不意味着变量没有类型。因为赋值时会自动匹配数据类型!
目前用到的基本数据类型
number
boolean
string
var i
i="test";//这时i就成了string类型
var i
i=4;//这时i就成了number类型
常用对象类型<object> ......
玩PHP、Delphi、Java基本上都有对象,习惯这种思路后上手任何语言都想靠OO思路,这绝不是在赶时髦,而是把相关代码进行内聚的确可以体会到维护的方便!
在JavaScript中如何创建对象?
JavaScript是基于对象的!它也是以Object为根类,其它类继承之。在根类提供了几个方法。供继承类使用!
以下是创建对象的例子:
funct ......
常规的方法是将年月日取出,然后分别判断范围,然后就判断闰年2月的天数
可以通过new Date(string)的构造,比较年月日字符是否发生变化判断。
function CheckDate(text) {
if (!text) return false;
text = text.replace(/[\/-]0?/g, "/");
if (!text.match(/^\d{4}\/\d{1,2}\/\d{1,2}$/)) return true;
......
A Note on Benchmarking 测试基准说明
Because a regex's performance can be wildly different depending on the text it's applied to, there's no straightforward way to benchmark regexes against each other. For the best result, you need to benchmark your regexes on test strings o ......
Yielding with Timers 用定时器让出时间片
Despite your best efforts, there will be times when a JavaScript task cannot be completed in 100 milliseconds or less because of its complexity. In these cases, it's ideal to yield control of the UI thread so that UI updates may occur ......