-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrow.js
42 lines (31 loc) · 1.18 KB
/
arrow.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
// Ways of writing arrow functions
// 1
const hello1 = (name) => { return "Hello, " + name + " !"; }
console.log(hello1("Jon"));
// 2
const hello2 = (name) => { return `Hello, ${name}`; } // template string
console.log(hello2("Rob"));
// 3
const hello3 = (name) => `Hello, ${name}`;
console.log(hello3("Arya"));
// 'this' problem with arrow functions
// an object
let ob = {
name: "Box",
description: () => { return `\nThis is an object called ${this.name}`; },
this_insideArrowFn: () => { return this; }, // -> this === window (or module.exports)
this_insideNormalFn: function () { return this; } // -> this === ob
};
// problem !!
console.log(ob.description()); // -> This is an object called undefined
// ^^^^^^^^^
// Cause: 'this' inside an arrow function refers
// to the global object and not the object in
// which the arraow function resides.
// See globalObject.js for more information on
// global 'this'
// Note:
console.log('\nValue of this inside an arrow fn in an object:');
console.log(ob.this_insideArrowFn());
console.log('\nValue of this inside a normal fn in an object:');
console.log(ob.this_insideNormalFn());