Skip to content

Instantly share code, notes, and snippets.

@codetilldie
Created November 14, 2013 17:12
Show Gist options
  • Save codetilldie/7470527 to your computer and use it in GitHub Desktop.
Save codetilldie/7470527 to your computer and use it in GitHub Desktop.
前端笔记之_js中this解析(未完待续,开坑待填)
在一个function中this指什么?
其实this指的是拥有该function的对象。
如果在一个html文档中直接声明一个function,那么它内部的this就是指向window。
以下资料参考自quirksmode.org:
Examples - copying
this is written into the onclick method in the following cases:
element.onclick = doSomething
element.addEventListener('click',doSomething,false)
element.onclick = function () {this.style.color = '#cc0000';}
<element onclick="this.style.color = '#cc0000';">
Examples - referring:
In the following cases this refers to the window:
element.onclick = function () {doSomething()}
element.attachEvent('onclick',doSomething)
<element onclick="doSomething()">
资料参考自:http://www.quirksmode.org/js/this.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment