[翻译]High Performance JavaScript(008)
Nested Members 嵌套成员
Since object members may contain other members, it's not uncommon to see patterns such as window.location.href in JavaScript code. These nested members cause the JavaScript engine to go through the object member resolution process each time a dot is encountered. Figure 2-12 shows the relationship between object member depth and time to access.
由于对象成员可能包含其它成员,例如不太常见的写法window.location.href这种模式。每遇到一个点号,JavaScript引擎就要在对象成员上执行一次解析过程。图2-12显示出对象成员深度与访问时间的关系。
Figure 2-12. Access time related to property depth
图2-12 访问时间与属性深度的关系
It should come as no surprise, then, that the deeper the nested member, the slower the data is accessed. Evaluating location.href is always faster than window.location.href, which is faster than window.location.href.toString(). If these properties aren't on the object instances, then member resolution will take longer as the prototype chain is searched at each point.
结果并不奇怪,成员嵌套越深,访问速度越慢。location.href总是快于window.location.href,而后者也要比window.location.href.toString()更快。如果这些属性不是对象的实例属性,那么成员解析还要在每个点上搜索原形链,这将需要更长时间。
Caching Object Member Values 缓存对象成员的值
With all of the performance issues related to object members, it's easy to believe that they should be avoided whenever possible. To be more accurate, you should be careful to use object member only when necessary. For instance, there's no reason to read the value of an object member more than once in a single function:
由于所有这些性能问题与对象成员有关,所以如果可能的话请避免使用它们。更确切地说,你应当小心地,只在必要情况下使用对象成员。例如,没有理由在一个函数中多次读取同一个对象成员的值:
function hasEitherClass(element, className1, className2){
return element.c
相关文档:
1.在HTML中使用<script>元素引入JavaScript。
该元素有两个属性,language声明要使用的脚本语言,src属性是可选的,用于引用外部JavaScript文件。
NB
:现在大多使用type属性(type=“text/javascript”)替代language属性,以便更好地支持XHTML(可扩展HTML)。
2.一般认为 ......
ZT:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html
上一次的文章,主要介绍了如何"封装"数据和方法,从原型对象生成实例。
今天要介绍的是,多个原型对象之间如何"继承"。
比如,现在有一个"动物"对象,
function Animal(){
this.species = "动物";
}
还有 ......
最近编写Javascript代码。起初没管那么多。一阵狂写。代码写得差不多了。结果上百K文件几十个。当然 没办法需要压缩了。为了速度。
找压缩工具。弄了下。结果错误一大堆。最后才发现是自己写的代码不规范导致的。检查了半天修正了几十个地方。终于能压缩了。
下面总结下需要注意的地方
1、对象结尾 function结尾 最 ......
<script>setTimeout("redirect('<?=$url_forward?>');", <?=$ms?>);</script>
设置分页:$page $pagesize $offset = ($page-1)*$pagesize;
$limit = "limit $offset,$pagesize" ......
**
* 我在网上看到过很多BASE64的JavaScript算法,都觉得不满意,于是自己写了一个,在这里分享一下。
* 我的代码在质量的效率都较高,没有一些冗余的操作。总体来讲我觉得非常不错。
* 如果大家有什么不懂的地方可以问我。
*/
var BASE64={
/**
* 此变量为编码的 ......