前端笔试题学习之06(this关键字)

var account={ phone:"12345678", getPhone:function(){ return this.phone; } }; var mycount=account.getPhone; console.log(account.getPhone()); console.log(mycount());

输出:
12345678
undefined
分析:
使用account.getPhone() 此时,this关键字指向account对象,所以输出:12345678
【前端笔试题学习之06(this关键字)】mycount() 表示只代表getPhone方法本身,此时的this关键字指向的当前的window对象,而window对象中是没有phone属性的,所以输出undefined

    推荐阅读