您的位置:首页 > 移动开发

javascript Call apply this 迟绑定

2012-07-03 09:37 916 查看
<!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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息