-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript6.js
129 lines (107 loc) · 3.82 KB
/
script6.js
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
console.log("Arrow functions");
/*Arrow functions are anonmous functions without a name and not bound with
an identifier.don,t return any value and can declare without function keyword.
It cannot be used as constructors.
The context within arrow functions is lexically or statically defined.
also called lambda functions in different languages.
Syntax of arrow functions:
let myFunction =(arg1,arg2,...argn) =>expression.
//arrow function with no argument
let greet =()=> console.log("hello");
greet();//hello
//with one argument
let greet =(name) => console.log(name);
greet('hello');
//as an expression(also create it dynamically)*/
// let age =5;
// let welcome =(age<15) ? () => console.log('baby'): () => console.log("adbul");
// welcome();//baby
// //MUltiline arrow function
// let sum =(a,b) => {
// let result = a+ b;
// return result;
// }
// let result1 = sum(3,5);
// console.log(result1);//8
//this with arrow function
//this keyword refers to the function where it is called.it is not associated with arrow functions.
//arrow functions does not have their own this.so whenever you call it refers to parent scope.
//inside a regular function
// function Person() {
// this.name="sandhya",
// this.age='25',
// this.sayName =function(){
// //this is accessiable
// console.log(this.age);//25
// //this refers to the global object
// function innerfunc(){
// console.log(this.age);//undefined
// console.log(name);//window object{}
// }
// innerfunc();
// }
// let x= new Person();
// x.sayName();
//Benifits of arrow functions
//2 major benefits:
//shorter syntax and require less code
//use of arrow function instead of normal function
//regular functions created a functions declarations or expressions
// are constructible and callable.
//this with arrow function
// function Person() {
// this.name="sandhya",
// this.age='25',
// this.sayName =function(){
// //this is accessiable
// console.log(this.age);//25
// //this refers to the global object
// function innerfunc(){
// console.log(this.age);//undefined
// console.log(this);//window object{}
// }
// innerfunc();
// }
// }
// let x= new Person();
// x.sayName();
// //this.name and this.age is accessible because this.sayname() is accessible is the method
// // of an Object.apply
// however,innerfunc() is a normal function and this.age is not accessible because this refers to the global object
// this.age in the innerfunc() gives undefined.
//inside an arrow function
// function Person() {
// this.name="sandhya",
// this.age='25',
// this.sayName =function(){
// //this is accessiable
// console.log(this.age);//25
// //this refers to the global object
// let innerfunc= () =>{
// console.log(this.age);//25
// }
// innerfunc();
// }
// }
// let x= new Person();
// x.sayName();
// //here innerfunc() is defined as an anonmouys function.Inside the arrow function,this refers
//to the parent,s scope.hence it gives 25.
//Arguments binding
//regular functions have arguments binding.that's why we pass arguments to a regular function
//it can access them using argumemts keyword,
// let x = function(){
// console.log(arguments);
// }
// x(4,5,6)//arguments
//arrow functions do not have argumnets binding.when you try to acess an argumnet
//using arrow function it will gives an error.
// let x =() =>{
// console.log(arguments);
// }
// x(4,5,6);//refrence error ,can't find variable argumnets
//to solve this issue we use spread operator
let x =(...n) =>{
console.log(n)
}
x(4,6,7);//[4,6,7]