美文网首页
this指向的面试题

this指向的面试题

作者: 有希望的活着 | 来源:发表于2020-05-17 16:51 被阅读0次

varname='王五';varobj={name:'张三',getName:function(){returnthis.name;},children:{name:'李四',getName:function(){returnthis.name;}}}console.log(obj.getName());//张三console.log(obj.children.getName());//李四vargetName=obj.children.getName;console.log(getName());//王五// 那么如果我想调用getName() 但是想打印张三怎么办 可以使用call函数改变调用当前函数的this指针console.log(getName.call(obj));//张三

相关文章

网友评论

      本文标题:this指向的面试题

      本文链接:https://www.haomeiwen.com/subject/xpysohtx.html