javascript prototype½éÉܵÄÎÄÕÂ
JavaScriptÊÇ»ùÓÚ¶ÔÏóµÄ£¬ÈκÎÔªËض¼¿ÉÒÔ¿´³É¶ÔÏó¡£È»¶ø£¬ÀàÐͺͶÔÏóÊDz»Í¬µÄ¡£±¾ÎÄÖУ¬ÎÒÃdzýÁËÌÖÂÛÀàÐͺͶÔÏóµÄһЩÌصãÖ®Í⣬¸üÖØÒªµÄÊÇÑо¿ÈçºÎд³öºÃµÄ²¢ÇÒÀûÓÚÖØÓõÄÀàÐÍ¡£±Ï¾¹£¬JavaScriptÕâÖÖÁ÷ÐеĽű¾ÓïÑÔÈç¹ûÄܹ»½øÐÐÁ¼ºÃµÄ·â×°£¬²¢ÐγÉÒ»¸öÅÓ´óµÄÀàÐͿ⣬¶ÔÓÚÖØÓÃÊǷdz£ÓÐÒâÒåµÄ¡£
ÍøÉ϶ÔÓÚprototypeµÄÎÄÕºܶ࣬һֱûÃ÷°×ºËÐĵÄ˼Ïë¡£×îºóдÁ˺ܶàÀý×Ó´úÂëºó²ÅÃ÷°×£ºprototypeÖ»ÄÜÓÃÔÚÀàÐÍÉÏ¡£
ÒÔÏÂÊÇһЩ¹ØÓÚÀàÐͺͶÔÏóµÄÀý×Ó£¬´ó¼Ò¿´ÍêÀý×Óºó¿ÉÄܸüÈÝÒ×Àí½âÀàÐͺͶÔÏóÖ®¼äµÄÁªÏµ£º
Àý×Ó´úÂë ˵Ã÷
1 Object.prototype.Property = 1;
Object.prototype.Method = function ()
{
alert(1);
}
var obj = new Object();
alert(obj.Property);
obj.Method();
¿ÉÒÔÔÚÀàÐÍÉÏʹÓÃproptotypeÀ´ÎªÀàÐÍÌí¼ÓÐÐΪ¡£ÕâЩÐÐΪֻÄÜÔÚÀàÐ͵ÄʵÀýÉÏÌåÏÖ¡£
JSÖÐÔÊÐíµÄÀàÐÍÓÐArray, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String
2 var obj = new Object();
obj.prototype.Property = 1; //Error
//Error
obj.prototype.Method = function()
{
alert(1);
}
ÔÚʵÀýÉϲ»ÄÜʹÓÃprototype£¬·ñÔò·¢Éú±àÒë´íÎó
3 Object.Property = 1;
Object.Method = function()
{
alert(1);
}
alert(Object.Property);
Object.Method();
¿ÉÒÔΪÀàÐͶ¨Ò哾²Ì¬”µÄÊôÐԺͷ½·¨£¬Ö±½ÓÔÚÀàÐÍÉϵ÷Óü´¿É
4 Object.Property = 1;
Object.Method = function()
{
alert(1);
}
var obj = new Object();
alert(obj.Property); //Error
obj.Method(); //Error
ʵÀý²»Äܵ÷ÓÃÀàÐ͵ľ²Ì¬ÊôÐÔ»ò·½·¨£¬·ñÔò·¢Éú¶ÔÏó䶨ÒåµÄ´íÎó¡£
5 function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
var obj = new Aclass();
alert(obj.Property);
obj.Method();
Õâ¸öÀý×ÓÑÝʾÁËͨ³£µÄÔÚJavaScriptÖж¨ÒåÒ»¸öÀàÐ͵ķ½·¨
6 function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
Aclass.prototype.Property2 = 2;
Aclass.prototype.Method2 = function
{
alert(2);
}
var obj = new Aclass();
alert(obj.Property2);
obj.Method2();
¿ÉÒÔÔÚÍ
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
A Note on Benchmarking ²âÊÔ»ù׼˵Ã÷
Because a regex's performance can be wildly different depending on the text it's applied to, there's no straightforward way to benchmark regexes against each other. For the best result, you need to benchmark your regexes on test strings o ......
Use the Fast Parts ʹÓÃËٶȿìµÄ²¿·Ö
Even though JavaScript is often blamed for being slow, there are parts of the language that are incredibly fast. This should come as no surprise, since JavaScript engines are built in lower-level languages and are therefore compiled. Thou ......
Summary ×ܽá
When web pages or applications begin to feel slow, analyzing assets as they come over the wire and profiling scripts while they are running allows you to focus your optimization efforts where they are needed most.
µ±ÍøÒ³»òÓ¦ÓóÌÐò±äÂýʱ£¬·ÖÎ ......