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,
AOP stands for Aspect-oriented programming. I think the main point of this technology is the ability to add code before or after a function execution.After some digging on the internet, i write my implement of AOP in JavaScript.1. First, see the
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
文章目錄 關於作者(The authors)貢獻者(Contributors)許可(License)中文翻譯(Chinese Translation)對象作為資料類型(Objects as a data type)訪問屬性(Accessing properties)刪除屬性(Deleting properties)屬性名稱的文法(Notation of keys)屬性尋找(Property lookup)原型屬性(The prototype
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