首页 > 其他 > 详细

this指向

时间:2016-09-29 07:35:35      阅读:99      评论:0      收藏:0      [点我收藏+]

1.

function BigComputer(answer) {
this.the_answer = answer;
this.ask_question = function () {
console.log(this.the_answer); //undefined
console.log(document.getElementById("btn") === this); //true
}
}

function addHandler () {
var deep_thought = new BigComputer(42);
the_button = document.getElementById("btn");
the_button.onclick = deep_thought.ask_question;
}

window.onload = addHandler;

ask_question中的this指向了dom元素节点,而节点中不包含the_answer信息,所以结果为undefined

2. apply()与call()方法:改变this指向

this指向

原文:http://www.cnblogs.com/susantong/p/5918486.html

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