1¡¢¶ÔÏóµÄ¼Ì³Ð£¬Ò»°ãµÄ×ö·¨ÊǸ´ÖÆ£ºObject.extend
prototype.jsµÄʵÏÖ·½Ê½ÊÇ£º
Object.extend = function(destination, source) { for (property in source) { destination[property] = source[property]; } return destination; }
³ý´ËÖ®Í⣬»¹ÓÐÖÖ·½·¨£¬¾ÍÊÇ£ºFunction.apply£¨µ±È»Ê¹ÓÃFunction.callÒ²ÊÇ¿ÉÒԵģ©
apply·½·¨ÄܽٳÖÁíÍâÒ»¸ö¶ÔÏóµÄ·½·¨£¬¼Ì³ÐÁíÍâÒ»¸ö¶ÔÏóµÄÊôÐÔ
Function.apply(obj,args)·½·¨ÄܽÓÊÕÁ½¸ö²ÎÊý
applyʾ·¶´úÂëÈçÏ£º
function Person(name,age){ //¶¨ÒåÒ»¸öÀ࣬ÈËÀà this.name=name; //Ãû×Ö this.age=age; //ÄêÁä this.sayhello=function(){alert("hello")}; } function Print(){ //ÏÔʾÀàµÄÊôÐÔ this.funcName="Print"; this.show=function(){ var msg=[]; for(var key in this){ if(typeof(this[key])!="function"){ msg.push([key,":",this[key]].join("")); } } alert(msg.join(" ")); }; } function Student(name,age,grade,school){ //ѧÉúÀà Person.apply(this,arguments); Print.apply(this,arguments); this.grade=grade; //Ä꼶 this.school=school; //ѧУ } var p1=new Person("jake",10); p1.sayhello(); var s1=new Student("tom",13,6,"Ç廪Сѧ"); s1.show(); s1.sayhello(); alert(s1.funcName);
ѧÉúÀà±¾À´²»¾ß±¸Èκη½·¨£¬µ«ÊÇÔÚPerson.apply(this,arguments)ºó£¬
Ëû¾Í¾ß±¸ÁËPersonÀàµÄsayhello·½·¨ºÍËùÓÐÊôÐÔ¡£
ÔÚPrint.apply(this,arguments)ºó¾Í×Ô¶¯µÃµ½ÁËshow()·½·¨
2¡¢ÀûÓÃApplyµÄ²ÎÊýÊý×黯À´Ìá¸ß
Function.apply()ÔÚÌáÉý³ÌÐòÐÔÄÜ·½ÃæµÄ¼¼ÇÉ
ÎÒÃÇÏÈ´ÓMath.max()º¯Êý˵Æð£¬Math.maxºóÃæ¿ÉÒÔ½ÓÈÎÒâ¸ö²ÎÊý£¬×îºó·µ»ØËùÓвÎÊýÖеÄ×î´óÖµ¡£
±ÈÈç
alert(Math.max(5,8))
alert(Math.max(5,7,9,3,1,6))
µ«ÊÇÔںܶàÇé¿öÏ£¬ÎÒÃÇÐèÒªÕÒ³öÊý×éÖÐ×î´óµÄÔªËØ¡£
var arr=[5,7,9,1]
alert(Math.max(arr))
function getMax(arr){
}
ÕâÑùдÂé·³¶øÇÒµÍЧ¡£Èç¹ûÓà applyÄØ£¬¿´´úÂ룺
function getMax2(arr){
}
Á½¶Î´úÂë´ïµ½ÁËͬÑùµÄÄ¿µÄ£¬µ«ÊÇgetMax2È´ÓÅÑÅ£¬¸ßЧ£¬¼ò½àµÃ¶à¡£
ÔÙ±ÈÈçÊý×éµÄpush·½·¨¡£
var arr1=[1,3,4];
var arr2=[3,4,5];
Èç¹ûÎÒÃÇÒª°Ñ arr2Õ¹¿ª£¬È»ºóÒ»¸öÒ»¸ö×·¼Óµ½arr1ÖÐÈ¥£¬×îºóÈÃarr1=[1,3,4,3,4,5]
arr1.push(arr2)ÏÔÈ»ÊDz»Ðеġ£ ÒòΪÕâÑù×ö»áµÃµ½[1,3,4,[3,4,5]]
ÎÒÃÇÖ»ÄÜÓÃÒ»¸öÑ»·È¥Ò»¸öÒ»¸öµÄpush(µ±È»Ò²¿ÉÒÔÓÃarr1.concat(arr2)£¬µ«ÊÇconcat·½·¨²¢²»¸Ä±äarr1±¾Éí)
var arrLen=arr2.length
for(var i=0;i<arrLen;i++){
}
×Ô´ÓÓÐÁËApply,ÊÂÇé¾Í±äµÃÈç´Ë¼òµ¥
Array.prototype.push.apply(arr1,arr2)
ÍƼöÐÅÏ¢
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- jQuery Tooltips²å¼þ
- jQueryµÄ.bind()¡¢.live()ºÍ.delegate(....
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- Ä㲻ϲ»¶»ðºü£¬µ«ÓÖϲ»¶firebug£¬ÄǾ͸úÎÒÒ»ÆðÀ´ÔÚIEÉÏ....
- ¸ù¾ÝÏÔʾÆ÷·Ö±æÂʵ÷ÕûÒ³ÃæÏÔʾµÄJavascript½Å±¾
- jQuery дµÄ·ÂÐÂÀË΢²© ÏòϹö¶¯Ð§¹û¡¾×ªÔØ¡¿
- ͨÓõÄJqueryÑ¡ÔñÆ÷
- jQuery ½áºÏ Json Ìá½»Êý¾Ýµ½Webservice£¬....
- ×ÔÖÆjQueryÖÇÄÜÌáʾ²å¼þһö
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- js»ñÈ¡srcÖд«µÝµÄ²ÎÊý
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- ÈÃä¯ÀÀÆ÷ÆÁ±ÎµôJavaScriptµÄ³ö´íÌáʾ
- github²»ÄܼÓÔØcss¡¢js½â¾ö°ì·¨
- ÎÒÖªµÀµÄJavaScript -- Éè¼Æģʽ(ÇŽÓ)Ó¦ÓÃÖ® ¨C ....
- 15¸öÖµµÃ¿ª·¢ÈËÔ±¹Ø×¢µÄjQuery¿ª·¢¼¼ÇɺÍÐĵÃ
- dz̸JavascriptÃæÏò¶ÔÏó±à³Ì
- JS¼òµ¥¶¯»·â×°
- JavaScript³õѧÕßӦעÒâµÄÆ߸öϸ½Ú
- jQuery Tooltips²å¼þ
ÆÀÂÛ