1. this指向是个老生常谈的问题,先总结如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var fullname = '1'
var obj = {
fullname: '2',
prop: {
fullname: '3',
getFullName: function() {
return this.fullname
}
}
}
console.log(obj.prop.getFullName())
var fun = obj.prop.getFullName
console.log(fun())
console.log(fun.call(obj))
</script>
</body>
</html>
控制台输出3,1,2
2. 结论
this实际上指向的是调用他的对象
1). 为obj.prop
2). 为window(因为全局作用域,所以省略未写)
3). 用call或apply可更改this指向
至此。