caller,callee,arguments,call,apply用法以及說明

<script>
//caller 得到呼叫自己的function
function callerDemo() {
if (callerDemo.caller) {
   var a= callerDemo.caller.toString(); 
   alert(a); 
} else { 
     alert("this is a top function"); 
}
}

function handleCaller() {
   callerDemo();
}
//callerDemo()
//handleCaller()

//arguments 獲得 當前方法需要的參數個數
//arguments.callee 獲得當前方法
//arguments.callee.length 獲得當前方法傳入參數的可是
function calleeDemo() { 
alert(arguments.callee);
}
function calleeLengthDemo(arg1, arg2){
     alert(arguments.callee.length)
if (arguments.length==arguments.callee.length) { 
   window.alert("驗證形參和實參長度正確!"); 
   return; 
} else { 
   alert("實參長度:" +arguments.length); 
   alert("形參長度: " +arguments.callee.length); 
}
}
//calleeDemo()
//calleeLengthDemo(10)


//用於實現function 的繼承
function simpleCallDemo(arg) {
window.alert(arg);
}
function handleSPC(arg) {
//把simpleCallDemo的所有方法賦值給當前對象 
//參數根據simpleCallDemo的個數加
simpleCallDemo.call(this, arg);
}
//handleSPC("111")
function simpleApplyDemo(arg) {
window.alert(arg);
}

function handleSPA(arg) {
//把simpleApplyDemo 的所有方法賦值給當前對象
//參數爲當前對象的參數(數組)
simpleApplyDemo.apply(this, arguments);
}
//handleSPA("111")

//實現對象的繼承
var test = {
value : 'default',
exec : function(){
alert(this.value);
}
}
function hhh(obj){
test.exec();
test.exec.apply(obj);
}

</script>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章