<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
var obj = {
id:"aw",
cool:function coolFn(){
console.log(this.id);
}
}
var id = 'not aw';
setTimeout(obj.cool,100);
</script>
</html>
为什么加了settimeout
以后输出的是全局变量的id
。
obj.cool 作为一个引用 传递个setTimeout, setTimeout 调用这个函数的时候是 '函数调用(而不是方法调用)' 所以this指向全局对象