传参的执行过程

<span style="font-size:18px;"><script>
    function Person(name,age){
<span style="white-space:pre">	</span>this.name = name;
<span style="white-space:pre">	</span>this.age = age;
<span style="white-space:pre">	</span>this.print = function(key){
<span style="white-space:pre">		</span>if(!!key){
<span style="white-space:pre">	</span>alert(key+':'+this[key]);
<span style="white-space:pre">	</span>return;
}
<span style="white-space:pre">	</span>for(var o in this){
<span style="white-space:pre">	</span>alert(o+':'+this[o]);
}
}
}
function Student(name,age,grade,school){
<span style="white-space:pre">	</span>this.name = name;
<span style="white-space:pre">	</span>this.age = age;
<span style="white-space:pre">	</span>this.grade = grade;
<span style="white-space:pre">	</span>this.school = school;
}
var person1 = new Person('老王',60);
var student1= new Student('小明',20,'大一','清华大学');
person1. print();
person1.print.apply(student1,['age']);
person1.print.call(student1,'grade','age');
 </script>
</span>

执行结果:

   name:老王  

   age:60

   print: function(key){

              if(!!key){

       alert(key+’:’+this[key]);

       return;

}

       for(var o in this){

       alert(o+’:’+this[o]);

}

}

  Age:20

  Grade:大一

执行顺序:1.执行person1.print:接收传递的nameage。  nameageprint放到o中遍历输出(因printkey没有接收的值,故将print后的值原样输出)

              2.  执行person1.print.apply(student1,[‘age’]);             student1中的age传参给print(即key),执行print等于的函数,判断有key,输出age

              3. 执行person1.print.call(student1,’grade’,’age’);      student1中的gradeage传参给print(即key,但是key只能接收一个grade值),执行print等于的函数,判断有key,输出grade


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值