javascript 优化
Optimizing JavaScript code
Authors: Gregory Baker, Software Engineer on GMail & Erik Arvidsson, Software Engineer on Google Chrome
Recommended experience: Working knowledge of JavaScript
Client-side
scripting can make your application dynamic and active, but the
browser's interpretation of this code can itself introduce
inefficiencies, and the performance of different constructs varies from
client to client. Here we discuss a few tips and best practices to
optimize your JavaScript code.
Working with strings
String
concatenation causes major problems with Internet Explorer 6 and 7
garbage collection performance. Although these issues have been
addressed in Internet Explorer 8 -- concatenating is actually slightly more
efficient on IE8 and other non-IE browsers such as Chrome -- if a
significant portion of your user population uses Internet Explorer 6 or
7, you should pay serious attention to the way you build your strings.
Consider this example:
var veryLongMessage =
'This is a long string that due to our strict line length limit of' +
maxCharsPerLine +
' characters per line must be wrapped. ' +
percentWhoDislike +
'% of engineers dislike this rule. The line length limit is for ' +
' style purposes, but we don't want it to have a performance impact.' +
' So the question is how should we do the wrapping?';
Instead of concatenation, try using a join:
var veryLongMessage =
['This is a long string that due to our strict line length limit of',
maxCharsPerLine,
' characters per line must be wrapped. ',
percentWhoDislike,
'% of engineers dislike this rule. The line length limit is for ',
' style purposes, but we don't want it to have a performance impact.',
' So the question is how should we do the wrapping?'
].join();
Similarly,
building up a string across conditional statements and/or loops by
using concatenation can be very inefficient. The wrong way:
var fibonacciStr = 'First 20 Fibonacci Numbers
';
for (var i = 0; i < 2
相关文档:
http://code.google.com/p/update-javascript-trim/
常见的写法为:
function trim(s){
return s.replace(/^\s+|\s+$/g, '');
}
优化后trim函数的代码如下:
function trim(str) {
str = str.replace(/^\s+/g, '');
for(var i = str.length - 1; i >= 0; i--){
if(!/^\s$/.test(str.substr(i,1) ......
在asp.net开发中,经常会用到后台和前台的交互,就此总结了一点c#和javascript相互操作的方法
1.在后台c#代码中调用jacascript的方法
javascript代码:
<script type="text/javascript" language="javascript">
function test()
{
alert("oec2003");
return false;
}
</s ......
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)><td>no</table> 可用于Table
2. <body onselectstart="return false"> 取消选取、防止复制
3. onpaste="return false" 不准粘贴
4. oncopy="return false;" oncut="return f ......
---->什么是类和对象
这是所有面向对象编程之前必须弄明白的.
所谓类:简单地说就是模板,说的专业一些,是一类具有某种性质的物的集合.比如:人就是一个类,车也是一个类,等等.
所谓对象:就是类的具体实现.如上面所说,人是一个类,一个具体的人就是一个对象,比如张三.
对象是类的实例化后的结果.ja ......