There is an excellent article named Optimizing JavaScript Code. The authors are software engineers on Gmailand Google chrome.I appreciate the knowledge described in this article and try to repeat it in myown words.Faster string concatenationI have
Produce boolean value from other typesAll objects in JavaScript can be converted to boolean implicitly, look at these examples:0 == false; // true1 == true; // true'' == false // truenull == false // truebut these values are not the type of
The observer pattern (a subset of the asynchronouspublish/subscribepattern) is a softwaredesign pattern in which an object,called the subject, maintains a list of its dependents, called observers, andnotifies them automatically of any state changes,
Always specify the second argument - parseIntparseInt converts a string to an int number, the syntax is:parseInt(str, [radix])The second argument is optional, which specify the radix of the first argument.If you omit radix, following the
Is undefined a reserved wordIt seems like so, but actually it doesn’t.var undefined = 'Hello'; undefined; // 'Hello'This may surprise you, but it does work well. undefined is just a pre-defined variable.Note: Never assign a value to undefined,
<script language="javascript" type="text/javascript"> var code ; //在全域 定義驗證碼 function createCode() { code = ""; var codeLength = 6;//驗證碼的長度 var checkCode = document.getElementById("checkCode"); var selectCha
function window.onbeforeunload(){if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey||event.ctrlKey){ //判斷event.altKey是為了Alt+F4關閉的情況;判斷event.ctrlKey是為了Ctrl+W關閉的情況
/*將String類型解析為Date類型. parseDate('2006-1-1') return new Date(2006,0,1) parseDate(' 2006-1-1 ') return new Date(2006,0,1) parseDate('2006-1-1 15:14:16') return new Date(2006,0,1,15,14,16) parseDate(' 2006-1-1 15:14:16 ') return new Dat
Regular Expression are very important in professional JavaScript.There is a wonderful resource in Mozilla developer center.Two ways to construct a regular expression 1. Literal expression re = /ab+c/g; 2. Constructor function re = new
JavaScript doesn’t have block scopeBlock doesn’t have scope in javascript, only function has scope.for(var i = 0; i < 2; i ++) { } i; // 2If you want to create scope, use anonymous function:(function (){ for(var i = 0; i < 2; i ++) { }
Fast string concatenationWe always use + to concatenate small strings into a large one, which is considered to be a good practice.But there will be a major hit on performance if you do many string concatenation operations on IE browser.Consider