[翻译]High Performance JavaScript(022)
第七章 Ajax 异步JavaScript和XML
Ajax is a cornerstone of high-performance JavaScript. It can be used to make a page load faster by delaying the download of large resources. It can prevent page loads altogether by allowing for data to be transferred between the client and the server asynchronously. It can even be used to fetch all of a page's resources in one HTTP request. By choosing the correct transmission technique and the most efficient data format, you can significantly improve how your users interact with your site.
Ajax是高性能JavaScript的基石。它可以通过延迟下载大量资源使页面加载更快。它通过在客户端和服务器之间异步传送数据,避免页面集体加载。它还用于在一次HTTP请求中获取整个页面的资源。通过选择正确的传输技术和最有效的数据格式,你可以显著改善用户与网站之间的互动。
This chapter examines the fastest techniques for sending data to and receiving it from the server, as well as the most efficient formats for encoding data.
本章考察从服务器收发数据最快的技术,以及最有效的数据编码格式。
Data Transmission 数据传输
Ajax, at its most basic level, is a way of communicating with a server without unloading the current page; data can be requested from the server or sent to it. There are several different ways of setting up this communication channel, each with its own advantages and restrictions. This section briefly examines the different approaches and discusses the performance implications of each.
Ajax,在它最基本的层面,是一种与服务器通讯而不重载当前页面的方法,数据可从服务器获得或发送给服务器。有多种不同的方法构造这种通讯通道,每种方法都有自己的优势和限制。本节简要地介绍这些不同方法,并讨论各自对性能的影响。
Requesting Data 请求数据
There are five general techniques for requesting data from a server:
有五种常用技术用于向服务器请求数据:
• XMLHttpRequest (XHR)
• Dynamic scrip
相关文档:
隐藏成员变量
在函数体内定义的变量为局部变量,离开函数就挂掉了
在函数体内使用this.成员变量名,则为window对象级变量,即全局变量
故需要这样隐藏成员变量,向外只暴露get、set函数
function testClass(name){
var _firstname=name;
return {
getname : function() {
return _fir ......
常规的方法是将年月日取出,然后分别判断范围,然后就判断闰年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;
......
Conditionals 条件表达式
Similar in nature to loops, conditionals determine how execution flows through JavaScript. The traditional argument of whether to use if-else statements or a switch statement applies to JavaScript just as it does to other languages. Since different b ......
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 ......