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();
¿ÉÒÔÔÚÍ
Ïà¹ØÎĵµ£º
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 ......
Splitting Up Tasks ·Ö½âÈÎÎñ
What we typically think of as one task can often be broken down into a series of subtasks. If a single function is taking too long to execute, check to see whether it can be broken down into a series of smaller functions that complete in smaller ......
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 ......
µÚ¾ÅÕÂ
Building and Deploying High-Performance JavaScript Applications
´´½¨²¢²¿Êð¸ßÐÔÄÜJavaScriptÓ¦ÓóÌÐò
According to a 2007 study by Yahoo!'s Exceptional Performance team, 40%–60% of Yahoo!'s users have an empty cache experience, and about 20% of all page views are done ......
JavaScript Minification JavaScript½ô´Õ
JavaScript minification is the process by which a JavaScript file is stripped of everything that does not contribute to its execution. This includes comments and unnecessary whitespace. The process typically reduces the file size by ha ......