[·Òë]High Performance JavaScript(018)
String Trimming ×Ö·û´®ÐÞ¼ô
Removing leading and trailing whitespace from a string is a simple but common task. Although ECMAScript 5 adds a native string trim method (and you should therefore start to see this method in upcoming browsers), JavaScript has not historically included it. For the current browser crop, it's still necessary to implement a trim method yourself or rely on a library that includes it.
È¥³ý×Ö·û´®Ê×βµÄ¿Õ¸ñÊÇÒ»¸ö¼òµ¥¶ø³£¼ûµÄÈÎÎñ¡£ËäÈ»ECMAScript 5Ìí¼ÓÁËÔÉú×Ö·û´®ÐÞ¼ôº¯Êý£¨ÄãÓ¦¸Ã¿ÉÒÔÔÚ¼´½«³öÏÖµÄä¯ÀÀÆ÷Öп´µ½ËüÃÇ£©£¬µ½Ä¿Ç°ÎªÖ¹JavaScript»¹Ã»Óаüº¬Ëü¡£¶Ôµ±Ç°µÄä¯ÀÀÆ÷¶øÑÔ£¬ÓбØÒª×Ô¼ºÊµÏÖÒ»¸öÐÞ¼ôº¯Êý£¬»òÕßÒÀ¿¿Ò»¸ö°üº¬´Ë¹¦ÄܵĿ⡣
Trimming strings is not a common performance bottleneck, but it serves as a decent case study for regex optimization since there are a variety of ways to implement it.
ÐÞ¼ô×Ö·û´®²»ÊÇÒ»¸ö³£¼ûµÄÐÔÄÜÆ¿¾±£¬µ«×÷ΪѧϰÕýÔò±í´ïʽÓÅ»¯µÄÀý×ÓÓжàÖÖʵÏÖ·½·¨¡£
Trimming with Regular Expressions ÓÃÕýÔò±í´ïʽÐÞ¼ô
Regular expressions allow you to implement a trim method with very little code, which is important for JavaScript libraries that focus on file size. Probably the best all-around solution is to use two substitutions—one to remove leading whitespace and another to remove trailing whitespace. This keeps things simple and fast, especially with long strings.
ÕýÔò±í´ïʽÔÊÐíÄãÓúÜÉٵĴúÂëʵÏÖÒ»¸öÐÞ¼ôº¯Êý£¬Õâ¶ÔJavaScript¹ØÐÄÎļþ´óСµÄ¿âÀ´ËµÊ®·ÖÖØÒª¡£¿ÉÄÜ×îºÃµÄÈ«Ãæ½â¾ö·½°¸ÊÇʹÓÃÁ½¸ö×Ó±í´ïʽ£ºÒ»¸öÓÃÓÚÈ¥³ýÍ·²¿¿Õ¸ñ£¬ÁíÒ»¸öÓÃÓÚÈ¥³ýβ²¿¿Õ¸ñ¡£ÕâÑù´¦Àí¼òµ¥¶øѸËÙ£¬ÌرðÊÇ´¦Àí³¤×Ö·û´®Ê±¡£
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+/, "").replace(/\s+$/, "");
}
}
// test the new method...
// tab (\t) and line feed (\n) characters are
// included in the leading whitespace.
var str = " \t\n test string ".trim();
alert(str == "tes
Ïà¹ØÎĵµ£º
JavaScriptʼþ´óÈ«
click() ¶ÔÏó.click() ʹ¶ÔÏó±»µã»÷¡£
closed ¶ÔÏó.closed ¶ÔÏó´°¿ÚÊÇ·ñÒѹرÕtrue/false
clearTimeout(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetTimeout¶ÔÏó
clearInterval(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetInterval¶ÔÏó
confirm("ÌáʾÐÅÏ¢") µ¯³öÈ·ÈÏ¿ò£¬È·¶¨·µ»ØtrueÈ¡Ïû·µ»Øfalse
cursor:Ñùʽ ¸ü¸ÄÊó±êÑùÊ ......
3¡¢×éºÏ¹¹Ô캯Êý/ÔÐÍ·½Ê½Ð´À࣬²ÉÓÃÇ°ÃæÖÖ·½Ê½¼Ì³Ð
ÕâÖÖ·½Ê½¸¸À࣬×ÓÀàµÄÊôÐÔ¶¼¹ÒÔÚ¹¹Ô캯ÊýÀ·½·¨¶¼¹ÒÔÚÔÐÍÉÏ¡£
/**
* ¸¸ÀàPolygon:¶à±ßÐÎ
*/
function Polygon(sides) {
this.sides = sides;
}
Polygon.prototype.setSides = function(s) {this.sides=s;}
/**
* Triangle Èý½ÇÐÎ
* @param {Object} b ......
[ת×Ô]http://article.yeeyan.org/view/mouse4x/16540
JSON±»¹«ÈÏΪä¯ÀÀÆ÷ÖÐXMLµÄºó¼ÌÕߣ¬ËüµÄÄ¿±ê½ö½öÊdzÉΪһÖÖ¼òµ¥¡¢ÓÅÑŵÄÊý¾Ý¸ñʽ£¬ÒÔ·½±ãä¯ÀÀÆ÷ºÍ·þÎñÆ÷Ö®¼äµÄÊý¾Ý½»»»¡£ÔÚÍê³ÉÕâÒ»¼òµ¥ÈÎÎñµÄ¹ý³ÌÖУ¬Ëü½«ÒýÁìÏÂÒ»´úÍòάÍø¡£
¶ÔÏó¼ò½é
¿´,Õâ¾ÍÊÇÒ»¸ö¶ÔÏó:
var myFirstObject ={};
¾¡¹Ü¿´ÆðÀ´Í¦¼òµ¥£¬È»¶øÄÇЩ» ......
typeof ÊÇJavaScriptµÄÔËËã·û
——·µ»ØÒ»¸öÓÃÀ´±íʾ±í´ïʽµÄÊý¾ÝÀàÐ͵Ä×Ö·û´®
typeof[(] expression [)] //typeofÓï·¨ÖеÄÔ²À¨ºÅÊÇ¿ÉÑ¡Ïî
typeof ÔËËã·û@import url(../html-vss/msdnie4a.css);
typeof
ÔËËã·û°ÑÀàÐÍÐÅÏ¢µ±×÷×Ö·û´®·µ»Ø¡£typeof
·µ»ØÖµÓÐÁ ......
Òþ²Ø³ÉÔ±±äÁ¿
ÔÚº¯ÊýÌåÄÚ¶¨ÒåµÄ±äÁ¿Îª¾Ö²¿±äÁ¿£¬À뿪º¯Êý¾Í¹ÒµôÁË
ÔÚº¯ÊýÌåÄÚʹÓÃthis.³ÉÔ±±äÁ¿Ãû£¬ÔòΪwindow¶ÔÏ󼶱äÁ¿£¬¼´È«¾Ö±äÁ¿
¹ÊÐèÒªÕâÑùÒþ²Ø³ÉÔ±±äÁ¿£¬ÏòÍâÖ»±©Â¶get¡¢setº¯Êý
function testClass(name){
var _firstname=name;
return {
getname : function() {
return _fir ......