Javascript this理解

原创 多啦cccc梦 随笔 js 153阅读 2018-05-11 10:04:14 举报

1.this指向调用函数的对象

例如:Jenny.callPerson(John),翻译成英文就是‘Jenny called a person named John’,callPerson()是由Jenny发起的,那this就指向Jenny
下面看一段例子

var name = 'Jenny Global';
var person ={
name:'Jenny',
details:{
  name:'Jenny details',
  print:function(){
    return this.name
}
},
print:function(){
 return this.name
}

}

console.log(person.details.print());//Jenny details
console.log(person.print());//Jenny
var name1= person.print;
var name2= person.details;
console.log(name1())//Jenny Global
console.log(name2.print())//Jenny details

关于箭头函数和普通函数的this指向区别,待理解后再记录

评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复