forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrowFunction.js
More file actions
62 lines (48 loc) · 1.64 KB
/
Copy patharrowFunction.js
File metadata and controls
62 lines (48 loc) · 1.64 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
let a = () => {
// This is arrow function came new in ES6
};
let info = {
firstName: "Swapnil",
lastName: "Shinde",
getFullName: () => {
return(`My name is ${this.firstName} ${this.lastName}`); // Arrow functions don't have "this" property
}
}
console.log(info.getFullName());
// Output My name is undefined undefined that's why we dont use this with arrow function
let newInfo = {
firstName: "Swapnil",
lastName: "Shinde",
getFullName: () => {
return(`My name is ${newInfo.firstName} ${newInfo.lastName}`); // If we are using arrow function then directly use the variables as shown
}
}
console.log(newInfo.getFullName());
// Output My name is Swapnil Shinde
// Using arrow functions in Class
class Student {
constructor() {
this.name = 'Vishal'
}
getName = () => {
return this.name;
}
}
console.log((new Student).getName()) // Gives error for node versions before 12.4.0(Approx) SyntaxError: Unexpected token =
class StudentInfo {
constructor (firstName,lastName, age, branch, college){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.branch = branch;
this.college = college;
};
getFullName = () => { // Returns full name using string interpolation
return(`My name is ${this.firstName} ${this.lastName}`); // If we are using arrow function then directly use the variables as shown
};
getBranch = () => { // Returns Branch
return(this.branch);
};
}
let Swapnil = new StudentInfo("Swapnil", "Shinde",19, "Computer", "Sies"); // This way we can create new objects with arguments
console.log(Swapnil.getFullName()); // Output My name is Swapnil Shinde