javascript Call apply this 迟绑定

本文探讨了JavaScript中对象方法的使用及作用域的概念,通过示例展示了如何在不同作用域下访问对象属性。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title> New Document </title>
  <script type="text/javascript">
window.onload = function(){
var _codeContent = document.getElementById('code').innerHTML;
eval(_codeContent);

}
</script>
 </head>


 <body><pre>
<div class="code" id="code">
(function(){
function People(){
this.id = 1;
this.name = 2;


this['getId'] = function(){
return this.id;
}
this['setId'] = function(id){
this.id = id;
}
this.getName = function(){
return this.name;
};
this.setName = function(name){
this.name = name;
};
}
function Student(){
this.sex;


this.getSex = function(){
return this.sex;
};


this.setSex = function(sex){
this.sex = sex;
};

var that = this;
this.show = function(arg1,arg2){
alert('用this.sex访问不到student的对象或方法,因为此时作用域是调用对象People\n 所以this.sex='+this.sex);
var arr = [];
arr.push('People ID属性:'+this.id);
arr.push('\nPeople Name属性:'+this.name);
arr.push('\nStudent Sex属性'+that.sex);
arr.push('\n调用时传入参数个数'+arguments.length);
alert(arr.join(''));
}
}


var people = new People();
var student = new Student();
student.setSex("femail");
student.show.call(people,'arg_01','arg_02');
/**student.show.apply(people,['arg_01','arg_02']);*/


})();
</div>
</pre>
 </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值