-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript-tutorial.js
More file actions
137 lines (109 loc) · 2.77 KB
/
javascript-tutorial.js
File metadata and controls
137 lines (109 loc) · 2.77 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// THIS
var person = "Erdinc";
window.person
// "Erdinc"
this.person
// "Erdinc"
// THIS 2
function example(){
var value = "data"
}
example();
// I can't reach the value because it is in the function scope's.
value
// Uncaught ReferenceError: value is not defined
// at <anonymous>:1:1
// THIS 3
function example2(){
this.value2 = "data2"
}
example2();
// I can't reach the value because it is in the function field.
// So here's how I reached "value2", we could use the keyword "this" because it was global (window) defined. But we should not actually need.
value2
// "data2"
window.value2
// "data2"
this.value2
// "data2"
// THIS 4
"use strict"
function example3(){
this.value3 = "data3";
}
// I use "use strict", even though i have linked value3 wtih global this, X
// I will not be able to access it because we ran it in "strict" mode.
// THIS 5
var person2 = {
name : "John",
meeting : function(){
return "Hi I'm " + this.name
}
}
var person2_two = {
name : "Jones",
meeting : function(){
return "Hi I'm " + this.name;
}
}
// Proper use
// It may not be very important in a small project,
// but using references becomes important in hundreds of lines of classes.
var person2_three = {
name : "Molly",
meeting : function(){
return "Hi I'm " + this.name;
}
}
var person2_four = {
name : "Lilly"
}
person2_three.meeting();
// "Hi I'm Molly"
person2_three.meeting.call(person2_four);
// "Hi I'm Lilly"
// CALL(), APPLY(), BIND()
// When using the Call () function, the arguments are sent to the function individually.
// Example: test (obj, arg1, arg2, arg3)
var ex1 = {num1:5, num2:10}
var ex2 = {num1:15, num2:20}
function addNumbers(num){
console.log(this.num1 + this.num2 + num);
}
addNumbers.call(ex1,50);
// 65
addNumbers.call(ex2,50);
// 85
// When using the Apply () function, the arguments are sent to the function as an argument list.
// Example: test (obj, [arg1, arg2, arg3])
var ex3 = {num1:5, num2:10}
var ex4 = {num1:15, num2:20}
function addNumbers2(num) {
console.log(this.num1 + this.num2 + num);
}
addNumbers2.apply(ex3,[50]);
// 65
addNumbers2.apply(ex4,[50]);
// 85
// What if we apply it to a real project?
function User(name){
this.name = name;
}
function Profile(name,age){
User.call(this,name);
this.age = age;
}
var ex5 = new Profile("John",15);
console.log(ex5.name);
// John
console.log(ex5.age);
// 15
// Bind() function may logically resemble call and apply, but is different in use.
// Bind() function creates a new copy according to the given object, and can use the object sent with the argument list.
var ex6 = {num1:5,num2:10};
function getTotal(num3,num4){
return this.num1 + this.num2 + num3 + num4;
}
var copyFunc = getTotal.bind(ex6);
console.log(copyFunc(20,25));
// 60