-
Notifications
You must be signed in to change notification settings - Fork 0
/
changeThis.html
53 lines (52 loc) · 1.62 KB
/
changeThis.html
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>this以及改变this的三大函数</title>
</head>
<body>
<script type="text/javascript">
//call与aplly的异同:
//1,第一个参数this都一样,指当前对象
//2,第二个参数不一样:call的是一个个的参数列表;apply的是一个数组(arguments也可以)
//绑定没有传递性,比如上面的嵌套的函数,a绑定到 o 对象,那么就影响了a函数,
var obj = {
a: function() {
//this==obj;
b = function(){
//this==window
}
}
}
var Person = function(name,age,like){
this.name = name;
this.age = age;
this.like = like;
this.say = function say(){
alert("我是:" + this.name);
}
}
var student = function(name,age,num){
this.num = num;
Person.call(this,name,age);
}
var father = function(name,like){
Person.apply(this,[name,like]);
}
var people = new Person("云流烟",21,"山水");
console.dir(people);
var stu = new student("蝉嘶雪",18,1408095052);
console.dir(stu);
var fa = new father("墨冥言","诗词");
console.dir(fa);
var a = function(){
name = 'a'
};
var b = function(){
console.log(this.name)
};
var c = b.bind(a);
c();
</script>
</body>
</html>