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
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,
/*將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
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 ++) { }
They are two similar code below:1.<code>function User(name) {this.name = name;}User.prototype = { 'sex': 'man' };var user = new User('Zhang');alert(User.prototype['sex']); // manalert(user.constructor.prototype['sex']); //
類 C 語言一般都用於塊級範圍,但是Javascript中的 while, if-else, for, switch-case 等控制結構不具有自己的範圍,在Javascript中只有函數(function)具有自己的範圍,請比較如下的 C# 代碼和 Javascript 代碼:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// C# 中 for