js的arguments,callee,caller,length,prototype屬性 我用幾個例子簡單的介紹下這幾個函數
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body>
<script type="text/javascript" >
function myFunc(arg1,arg2){};
alert(myFunc.length);//顯示函數形參的個數 結果爲2
//------------------------------
function myfunc2(){
var s="";
var len=arguments.length;
for (var i=0;i<len;i++){
s+=arguments[i];//此處要注意 arguments並非真正的數組,如果需要變爲數組,下面有方式方法。
//var args=Array.prototype.slice.call(arguments);//將arguments轉變成真正的數組,並賦予args
};
alert(s);
};
myfunc2(1,2,3);//結果爲123
(function test(){
alert(arguments.callee);//arguments.callee指的就是函數自己 注意 callee是 arguments的屬性,不是函數的的
})();
//------------------------------
function printStackTrace(fn){//此函數可以找到函數的調用軌跡
var s="";
while(fn.caller){
s+=fn.caller;//fn.caller查看函數的被調用者
s+="\n<--\n";
fn=fn.caller;
};
alert(s);
};
function test3(){
printStackTrace(test3);
};
function test2(){
test3();
};
function test1(){
test2();
};
function test0(){
test1();
};
test0();
//------------------------------
</script>
</body>
</html>