-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmethods.js
More file actions
61 lines (53 loc) · 1.8 KB
/
methods.js
File metadata and controls
61 lines (53 loc) · 1.8 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
/**
* Run this file by using "node methods"
*
* A method is a function that lies inside
* objects, they usually used to modify the
* object value, or just a collection of
* function that organized into one object
*/
let user = {
name: "Nanda",
age: 17,
address: {
city: 'Sleman',
province: "DIY"
},
friends: ['Jauza', 'Wildan'],
// 👇 how to write a method
greet() {
/**
* By writing a method, we will have a special access
* to a code called "this"
*
* "this" refers to current object that have the method
* so if method greet() is inside user object, then
* "this" will refer to user object
*/
return `Hello! My name is ${this.name}, my age is ${this.age}
I live in ${this.address.city} and I have many friends such as ${this.friends.join(', ')}`
}
}
console.log(user.greet());
/**
* In fact, all of data types in Javascript are objects
*/
// Methods in string values
let string = "this is string"
console.log(".toUpperCase():", string.toUpperCase());
console.log(".substr():", string.substr(5));
// More at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#instance_methods
// Methods in number values
let number = 1.21345
console.log(".toFixed():", number.toFixed(2));
// More at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
// There is no usable method in Boolean
// Methods in array
let friends = ['Akbar', "Siti", "Winda"]
console.log(".join():", friends.join());
console.log(".includes():", friends.includes("Siti"));
console.log(".map():", friends.map((friend) => {
return `${friend} mah bro`
}));
// More at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods
// In object, we create our own method 😁