代码如下:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><body> <div id="div0"></div></body><script> window.id = 'window'; document.getElementById('div0').onclick = function(){ alert(this.id); var callback = function(){ alert(this.id) } callback(); };</script></html>
第一个输出"div0"可以理解很正常,为何第二个输出的是"window",callback是内部的函数他应该和外部的this都隐式调用指向div0,为何外部指向div0,内部指向window?
于是尝试命令行运行:
window.name = "window"var text = function(){ name = "text"; alert(this.name); var callback = function(){ alert(this.name) } callback() } text()
结果输出两次"text"
两者矛盾了吧,已晕望大神指点
相关分类