-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththisdemo.html
More file actions
43 lines (40 loc) · 1.16 KB
/
thisdemo.html
File metadata and controls
43 lines (40 loc) · 1.16 KB
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
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
var myvar =100;
function WhoIsThis(){
var myvar =200;
alert("myVar = " + myvar);
alert("this.myVar = " +this.myvar);
}
WhoIsThis(); //window.WhoIsThis() also works the same way
var obj = new WhoIsThis();
alert(obj.myvar);
// example 2
var myvar1 = 1000;
var obj = {
myvar1 : 3000,
WhoIsThis:function(){
var myvar1 = 2000;
alert (myvar1); //2000
alert(this.myvar1); //3000
}
};
obj.WhoIsThis();
//call() and apply()
var demo = 100; //window Object
function getData(){
alert(this.demo);
}
// demo and getdata is the obj1's property
var obj1 = {demo:200 , getdata :getData};
var obj2 = {demo:300 , getdata :getData};
getData(); // this will point to window object so it will show 100
getData.call(obj1); // it will point to obj1 so it will show 200
getData.apply(obj2);// this will point to obj2 so it will show 300
obj1.getdata.call(window); // this will point to window object so it will show 100
</script>
</body>
</html>