`
Neil_yang
  • 浏览: 126292 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

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>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics