Vit*_*.us 2 javascript scope this
如何转发this范围引用调用事件监听器的元素?
为例:
<input type=button value="Foo" id=mybutton>
Run Code Online (Sandbox Code Playgroud)
addEvent('mybutton','touchstart', function(){
if(window['touchmoved']==false)
hover();
});
function hover(){
this.value //undefined
}
Run Code Online (Sandbox Code Playgroud)
使用JavaScript Function.call或Function.apply方法:
addEvent('mybutton','touchstart', function(){
if(window['touchmoved']==false)
hover.call(this); // `this` of the called hover points to this `this`
});
Run Code Online (Sandbox Code Playgroud)