码迷,mamicode.com
首页 > 其他好文 > 详细

this指向

时间:2016-09-29 07:35:44      阅读:104      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!