JavaScript apply¡¢call·½·¨ºÍlengthÊôÐÔ
JavaScriptΪº¯Êý¶ÔÏó¶¨ÒåÁËÁ½¸ö·½·¨£ºapplyºÍcall£¬ËüÃǵÄ×÷Óö¼Êǽ«º¯Êý°ó¶¨µ½ÁíÍâÒ»¸ö¶ÔÏóÉÏÈ¥ÔËÐУ¬Á½Õß½öÔÚ¶¨Òå²ÎÊýµÄ·½Ê½ÓÐËùÇø±ð£º
Function.prototype.apply(thisArg,argArray);
Function.prototype.call(thisArg[,arg1[,arg2…]]);
´Óº¯ÊýÔÐÍ¿ÉÒÔ¿´µ½£¬µÚÒ»¸ö²ÎÊý¶¼±»È¡ÃûΪthisArg£¬¼´ËùÓк¯ÊýÄÚ²¿µÄthisÖ¸Õ붼»á±»¸³ÖµÎªthisArg£¬Õâ¾ÍʵÏÖÁ˽«º¯Êý×÷ΪÁíÍâÒ»¸ö¶ÔÏóµÄ·½·¨ÔËÐеÄÄ¿µÄ¡£Á½¸ö·½·¨³ýÁËthisArg²ÎÊý£¬¶¼ÊÇΪFunction¶ÔÏ󴫵ݵIJÎÊý¡£ÏÂÃæµÄ´úÂë˵Ã÷ÁËapplyºÍcall·½·¨µÄ¹¤×÷·½Ê½£º
function func1(){
this.p="func1-";
this.A=function(arg){
alert(this.p+arg);
}
}
function func2(){
this.p="func2-";
this.B=function(arg){
alert(this.p+arg);
}
}
var obj1=new func1();
var obj2=new func2();
obj1.A("byA"); //ÏÔʾfunc1-byA
obj2.B("byB"); //ÏÔʾfunc2-byB
obj1.A.apply(obj2,["byA"]); //ÏÔʾfunc2-byA£¬ÆäÖÐ[“byA”]ÊǽöÓÐÒ»¸öÔªËصÄÊý×飬ÏÂͬ
obj2.B.apply(obj1,["byB"]); //ÏÔʾfunc1-byB
obj1.A.call(obj2,"byA"); //ÏÔʾfunc2-byA
obj2.B.call(obj1,"byB"); //ÏÔʾfunc1-byB
¿ÉÒÔ¿´³ö£¬obj1µÄ·½·¨A±»°ó¶¨µ½obj2ÔËÐкó£¬Õû¸öº¯ÊýAµÄÔËÐл·¾³¾ÍתÒƵ½ÁËobj2£¬¼´thisÖ¸ÕëÖ¸ÏòÁËobj2¡£Í¬Ñùobj2µÄº¯ÊýBÒ²¿ÉÒ԰󶨵½obj1¶ÔÏóÈ¥ÔËÐС£´úÂëµÄ×îºó4ÐÐÏÔʾÁËapplyºÍcallº¯Êý²ÎÊýÐÎʽµÄÇø±ð¡£
ÓëargumentsµÄlengthÊôÐÔ²»Í¬£¬º¯Êý¶ÔÏó»¹ÓÐÒ»¸öÊôÐÔlength£¬Ëü±íʾº¯Êý¶¨ÒåʱËùÖ¸¶¨²ÎÊýµÄ¸öÊý£¬¶ø·Çµ÷ÓÃʱʵ¼Ê´«µÝµÄ²ÎÊý¸öÊý¡£ÀýÈçÏÂÃæµÄ´úÂ뽫ÏÔʾ2£º
function sum(a,b){
return a+b;
}
alert(sum.length);
Ïà¹ØÎĵµ£º
¿Í»§¶Ë¶¯Ì¬Êä³ötableÊý¾Ý²¢Õ¹Ê¾±í¸ñ£¬ÊÇwebÓ¦ÓÃÖнÏΪ³£¼ûµÄ¹¤×÷¡£¶ÔÓÚÑ»·´òÓ¡Êä³ötr£¬td±¾ÉíÊÇÒ»¼þ·Ç³£½©Ó²ºÍ±©Á¦µÄ±à³Ì°ì·¨£¬ÔÙ¼ÓÉÏ×îºó°ó¶¨ÔªËØinnerHTML×Ö·ûÁ÷Êä³ö£¬
¡¡¡¡ÏµÍ³ËùÏûºÄµÄÐÔÄÜ´ú¼ÛÊǷdz£¸ß°ºµÄ£¬Èç¹ûÎÒÃÇÐèÒªÕ¹ÏÖµÄÊý¾Ý·Ç³£ÅÓ´óʱ£¬ÄÇô´ú¼ÛÒ²Êdzɱ¶µÄ¡£È»¶øÕâÖÖ¶¯Ì¬Êä³ö±í¸ñµÄ·½·¨ÊÇ´ó¶àÊý¿Í»§¶Ë³ÌÐòÔ ......
function getOs(){
var OsObject = "";
if(navigator.userAgent.indexOf("MSIE")>0) {
return "MSIE";
}
if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
&n ......
Ò³ÃæÄÚǶ²»Í¬µÄÍøÒ³ÄÚÈÝʱ£¬µ¼ÖÂiframeµÄ¸ß¶ÈÎÞ·¨ÓëʵÏÖÄÚÈÝ»òä¯ÀÀÆ÷¸ß¶ÈÏàÆ¥Åä¡£´°¿Ú´óС±ä»»Ê±£¬²»ÄÜiframe²»ÄܽøÐÐ×Ô¶¯µ÷Õû¡£ÕâÀÎÒÃÇÀûÓÃJAVASCRIPTÈÃIFRAME¿ò¼ÜµÄ¸ß¶È×ÔÊÊÓ¦¡£
Ê×ÏÈ£¬Ð´³öÈçϵÄJS·½·¨¡£
/**
* ÄÚǶҳÃæµÄ¸ß¶ÈÉèÖÃ
*/
function handleFrameLoad() {
var hDoc = getBodyHeight(document);
var ......
ÍøÒ³¿É¼ûÇøÓò¿í£º document.body.clientWidth
ÍøÒ³¿É¼ûÇøÓò¸ß£º document.body.clientHeight
ÍøÒ³¿É¼ûÇøÓò¿í£º document.body.offsetWidth (°üÀ¨±ßÏߵĿí)
ÍøÒ³¿É¼ûÇøÓò¸ß£º document.body.offsetHeight (°üÀ¨±ßÏߵĸß)
ÍøÒ³ÕýÎÄÈ«ÎÄ¿í£º document.body.scrollWidth
ÍøÒ³ÕýÎÄÈ«Îĸߣº document.body.scrollHeight ......