首页 > 其他 > 详细

this

时间:2017-09-09 15:02:50      阅读:212      评论:0      收藏:0      [点我收藏+]
(1) /*通过this传递了对象的引用*/
function identify() {
console.log(this); //this指向不是window 而是call改变指向的对象
return this.name.toUpperCase();

}
function speak() {
var greeting =‘hello i am‘+identify.call(this);
return greeting;

}
var you={
name:‘zhangsan‘
};
let me={
name:‘hxq‘
};
// console.log(identify.call(me));
// console.log(identify.call(me)); //HXQ
// console.log(identify.call(you)); //ZHANGSAN

console.log(speak.call(me)) //HXQ


2))
!(function () {
/*误解:this指向函数本身*/
function fn(n) {
console.log(n);
this.count++;//NAN
}
fn.count=0;


var i;
for (i=0;i<10;i++){
if(i>5){
fn(i);
}
}
console.log(fn.count);//o
})()

this

原文:http://www.cnblogs.com/aqigogogo/p/7498171.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!