var Person1= {
name:"tom",
say:function(){
alert("my name is "+this.name);
}
}
var Person2= {
name:"cat",
say:function(){
alert("我是 "+this.name);
}
}
Person1.say();//my name is tom
Person1.say.call(Person2);//my name is cat
var empty ={};
Person1.say.call(empty);//my name is undefined
/*call说明:对象1.方法A.call(对象2)的作用是,让对象2替代本身对象1,如果对象1涉及的属性对象2不存在,则视为undeifend*/
<input type="text" id="myText" value="将文本框作为对象">
<script>
function Obj(){this.value="Obj变量";}
var value="windows 全局 变量";
var Fun1={
//全局的this为window
value:"内部方法全局变量",
test:function(msg){
alert(msg+":"+this.value);
}
}
Fun1.test("全局变量"); //内部方法全局变量
Fun1.test.call(window,"将window作为对象"); //windows 全局 变量
Fun1.test.call(document.getElementById('myText'),"将文本框作为对象"); //input text
Fun1.test.call(new Obj(),"将方法作为对象"); //Obj变量
</script>
call小结
最新推荐文章于 2023-06-01 09:52:28 发布
2万+

被折叠的 条评论
为什么被折叠?



