ECMAScript 6 对象的扩展之this => 浅谈

种奔奔

看一下以下代码 注意this的指向


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<script type="text/javascript">
var ageInt = 10;
var ob = {
name:"zs",
age:ageInt
}

var age = 10;
var ob2 = {age:age}
dw(ob2.age);//10
var ob1={age};
dw(ob1.age);//10

var ob3={
age:20,
showAge:function(){
alert(this.age); //20 this->object
},
showAgeX(){
alert(this.age); //20 this->object
},
showAgeXX:()=>{
alert(this.age); //10 this->window
}
}
ob3.showAge();
ob3.showAgeX();
ob3.showAgeXX();


function dw(param){
document.write(param);
document.write("<br />")
}
</script>

ECMAScript中新增了很多不一样的用法 更多内容 -> 查看