You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/15-function-basics/article.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,7 +181,11 @@ Dengan kata lain, untuk meluruskan istilah-istilah ini:
181
181
- Parameter adalah variabel yang tercantum di dalam tanda kurung dalam deklarasi fungsi (ini adalah istilah waktu deklarasi)
182
182
- Argumen adalah nilai yang diteruskan ke fungsi saat dipanggil (ini adalah istilah waktu panggilan).
183
183
184
+
<<<<<<< HEAD
184
185
Kami mendeklarasikan fungsi yang mencantumkan parameternya, lalu memanggilnya lewat argumen.
186
+
=======
187
+
In the example above, one might say: "the function `showMessage` is declared with two parameters, then called with two arguments: `from` and `"Hello"`".
188
+
>>>>>>> 4d01fc20d4d82358e61518a31efe80dec9bb2602
185
189
186
190
Dalam contoh di atas, seseorang mungkin mengatakan: "fungsi `sayMessage` dideklarasikan dengan dua parameter, kemudian dipanggil dengan dua argumen: `from` dan `"Hello"`".
187
191
@@ -250,7 +254,11 @@ function showMessage(text) {
250
254
showMessage(); // empty message
251
255
```
252
256
257
+
<<<<<<< HEAD
253
258
...Atau kita bisa menggunakan operator `||`:
259
+
=======
260
+
...Or we could use the `||` operator:
261
+
>>>>>>> 4d01fc20d4d82358e61518a31efe80dec9bb2602
254
262
255
263
```js
256
264
// jika teks parameter tidak ada atau "", set variabel ke 'empty'
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/06-polyfills/article.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,11 @@ di chapter ini, tujuan kita adalah untuk mendapatkan intisari cara kerjanya, dan
21
21
22
22
## Transpilers
23
23
24
+
<<<<<<< HEAD
24
25
Sebuah [transpiler](https://en.wikipedia.org/wiki/Source-to-source_compiler) adalah perangkat lunak khusus yang dapat mengurai ("membaca dan memahami") kode modern, dan menulis ulang menggunakan konstruksi sintaks yang lebih lama, sehingga hasilnya akan sama.
26
+
=======
27
+
A [transpiler](https://en.wikipedia.org/wiki/Source-to-source_compiler) is a special piece of software that translates source code to another source code. It can parse ("read and understand") modern code and rewrite it using older syntax constructs, so that it'll also work in outdated engines.
28
+
>>>>>>> 4d01fc20d4d82358e61518a31efe80dec9bb2602
25
29
26
30
Misalnya. JavaScript sebelum tahun 2020 tidak memiliki "nullish coalescing operator" `??`. Jadi, jika pengunjung menggunakan browser yang sudah ketinggalan zaman, ia mungkin gagal memahami kode seperti `height = height ?? 100`
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/09-object-toprimitive/article.md
+185-1Lines changed: 185 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,11 @@ Itu batasan penting, karena hasil dari `obj1 + obj2` tidak bisa menjadi objek la
10
10
11
11
Misalnya. kita tidak dapat membuat objek yang mewakili vektor atau matriks (atau pencapaian atau apa pun), menambahkannya dan mengharapkan objek "dijumlahkan" sebagai hasilnya. Prestasi arsitektur seperti itu secara otomatis "di luar papan".
12
12
13
+
<<<<<<< HEAD
13
14
Jadi, karena kita tidak bisa berbuat banyak di sini, tidak ada matematika dengan objek dalam proyek nyata. Ketika itu terjadi, biasanya karena kesalahan pengkodean.
15
+
=======
16
+
E.g. we can't make objects representing vectors or matrices (or achievements or whatever), add them and expect a "summed" object as the result. Such architectural feats are automatically "off the board".
17
+
>>>>>>> 4d01fc20d4d82358e61518a31efe80dec9bb2602
14
18
15
19
Dalam bab ini kita akan membahas bagaimana sebuah objek dikonversi ke primitif dan bagaimana menyesuaikannya.
16
20
@@ -97,5 +101,185 @@ Tidak ada petunjuk "boolean" (semua objek `benar` dalam konteks boolean) atau ya
97
101
Mari kita mulai dari cara pertama. Ada simbol bawaan bernama `Symbol.toPrimitive` yang harus digunakan untuk menamai metode konversi, seperti ini:
98
102
99
103
```js
104
+
<<<<<<< HEAD
100
105
obj[Simbol.toPrimitif] = fungsi(petunjuk) {
101
-
// dia
106
+
// dia
107
+
=======
108
+
obj[Symbol.toPrimitive] = function(hint) {
109
+
// here goes the code to convert this object to a primitive
110
+
// it must return a primitive value
111
+
// hint = one of "string", "number", "default"
112
+
};
113
+
```
114
+
115
+
If the method `Symbol.toPrimitive` exists, it's used for all hints, and no more methods are needed.
116
+
117
+
For instance, here `user` object implements it:
118
+
119
+
```js run
120
+
let user = {
121
+
name:"John",
122
+
money:1000,
123
+
124
+
[Symbol.toPrimitive](hint) {
125
+
alert(`hint: ${hint}`);
126
+
return hint =="string"?`{name: "${this.name}"}`:this.money;
127
+
}
128
+
};
129
+
130
+
// conversions demo:
131
+
alert(user); // hint: string -> {name: "John"}
132
+
alert(+user); // hint: number -> 1000
133
+
alert(user +500); // hint: default -> 1500
134
+
```
135
+
136
+
As we can see from the code, `user` becomes a self-descriptive string or a money amount depending on the conversion. The single method `user[Symbol.toPrimitive]` handles all conversion cases.
137
+
138
+
139
+
## toString/valueOf
140
+
141
+
If there's no `Symbol.toPrimitive` then JavaScript tries to find methods `toString` and `valueOf`:
142
+
143
+
- For the "string" hint: `toString`, and if it doesn't exist, then `valueOf` (so `toString` has the priority for string conversions).
144
+
- For other hints: `valueOf`, and if it doesn't exist, then `toString` (so `valueOf` has the priority for maths).
145
+
146
+
Methods `toString` and `valueOf` come from ancient times. They are not symbols (symbols did not exist that long ago), but rather "regular" string-named methods. They provide an alternative "old-style" way to implement the conversion.
147
+
148
+
These methods must return a primitive value. If `toString` or `valueOf` returns an object, then it's ignored (same as if there were no method).
149
+
150
+
By default, a plain object has following `toString` and `valueOf` methods:
151
+
152
+
- The `toString` method returns a string `"[object Object]"`.
153
+
- The `valueOf` method returns the object itself.
154
+
155
+
Here's the demo:
156
+
157
+
```js run
158
+
let user = {name:"John"};
159
+
160
+
alert(user); // [object Object]
161
+
alert(user.valueOf() === user); // true
162
+
```
163
+
164
+
So if we try to use an object as a string, like in an `alert` or so, then by default we see `[object Object]`.
165
+
166
+
The default `valueOf` is mentioned here only for the sake of completeness, to avoid any confusion. As you can see, it returns the object itself, and so is ignored. Don't ask me why, that's for historical reasons. So we can assume it doesn't exist.
167
+
168
+
Let's implement these methods to customize the conversion.
169
+
170
+
For instance, here `user` does the same as above using a combination of `toString` and `valueOf` instead of `Symbol.toPrimitive`:
171
+
172
+
```js run
173
+
let user = {
174
+
name:"John",
175
+
money:1000,
176
+
177
+
// for hint="string"
178
+
toString() {
179
+
return`{name: "${this.name}"}`;
180
+
},
181
+
182
+
// for hint="number" or "default"
183
+
valueOf() {
184
+
returnthis.money;
185
+
}
186
+
187
+
};
188
+
189
+
alert(user); // toString -> {name: "John"}
190
+
alert(+user); // valueOf -> 1000
191
+
alert(user +500); // valueOf -> 1500
192
+
```
193
+
194
+
As we can see, the behavior is the same as the previous example with `Symbol.toPrimitive`.
195
+
196
+
Often we want a single "catch-all" place to handle all primitive conversions. In this case, we can implement `toString` only, like this:
197
+
198
+
```js run
199
+
let user = {
200
+
name:"John",
201
+
202
+
toString() {
203
+
returnthis.name;
204
+
}
205
+
};
206
+
207
+
alert(user); // toString -> John
208
+
alert(user +500); // toString -> John500
209
+
```
210
+
211
+
In the absence of `Symbol.toPrimitive` and `valueOf`, `toString` will handle all primitive conversions.
212
+
213
+
### A conversion can return any primitive type
214
+
215
+
The important thing to know about all primitive-conversion methods is that they do not necessarily return the "hinted" primitive.
216
+
217
+
There is no control whether `toString` returns exactly a string, or whether `Symbol.toPrimitive` method returns a number for a hint `"number"`.
218
+
219
+
The only mandatory thing: these methods must return a primitive, not an object.
220
+
221
+
```smart header="Historical notes"
222
+
For historical reasons, if `toString` or `valueOf` returns an object, there's no error, but such value is ignored (like if the method didn't exist). That's because in ancient times there was no good "error" concept in JavaScript.
223
+
224
+
In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise there will be an error.
225
+
```
226
+
227
+
## Further conversions
228
+
229
+
As we know already, many operators and functions perform type conversions, e.g. multiplication `*` converts operands to numbers.
230
+
231
+
If we pass an object as an argument, then there are two stages:
232
+
1. The object is converted to a primitive (using the rules described above).
233
+
2. If the resulting primitive isn't of the right type, it's converted.
234
+
235
+
For instance:
236
+
237
+
```js run
238
+
let obj = {
239
+
// toString handles all conversions in the absence of other methods
240
+
toString() {
241
+
return"2";
242
+
}
243
+
};
244
+
245
+
alert(obj *2); // 4, object converted to primitive "2", then multiplication made it a number
246
+
```
247
+
248
+
1. The multiplication `obj * 2` first converts the object to primitive (that's a string `"2"`).
249
+
2. Then `"2" * 2` becomes `2 * 2` (the string is converted to number).
250
+
251
+
Binary plus will concatenate strings in the same situation, as it gladly accepts a string:
252
+
253
+
```js run
254
+
let obj = {
255
+
toString() {
256
+
return"2";
257
+
}
258
+
};
259
+
260
+
alert(obj +2); // 22 ("2" + 2), conversion to primitive returned a string => concatenation
261
+
```
262
+
263
+
## Summary
264
+
265
+
The object-to-primitive conversion is called automatically by many built-in functions and operators that expect a primitive as a value.
266
+
267
+
There are 3 types (hints) of it:
268
+
-`"string"` (for `alert` and other operations that need a string)
269
+
-`"number"` (for maths)
270
+
-`"default"` (few operators)
271
+
272
+
The specification describes explicitly which operator uses which hint. There are very few operators that "don't know what to expect" and use the `"default"` hint. Usually for built-in objects `"default"` hint is handled the same way as `"number"`, so in practice the last two are often merged together.
273
+
274
+
The conversion algorithm is:
275
+
276
+
1. Call `obj[Symbol.toPrimitive](hint)` if the method exists,
277
+
2. Otherwise if hint is `"string"`
278
+
- try `obj.toString()` and `obj.valueOf()`, whatever exists.
279
+
3. Otherwise if hint is `"number"` or `"default"`
280
+
- try `obj.valueOf()` and `obj.toString()`, whatever exists.
281
+
282
+
In practice, it's often enough to implement only `obj.toString()` as a "catch-all" method for string conversions that should return a "human-readable" representation of an object, for logging or debugging purposes.
283
+
284
+
As for math operations, JavaScript doesn't provide a way to "override" them using methods, so real life projects rarely use them on objects.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/article.md
+27-2Lines changed: 27 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,11 @@ Dalam JavaScript modern, ada dua tipe angka:
4
4
5
5
1. Angka regular di JavaScript yang disimpan dalam format 64-bit [IEEE-754](https://en.wikipedia.org/wiki/IEEE_754-2008_revision), juga dikenal sebagai "angka double precision floating point". Inilah angka yang kita paling sering kita pakai, dan kita akan bahas tentang mereka di bab ini.
6
6
7
+
<<<<<<< HEAD
7
8
2. Angka BigInt, untuk mewakili integer dengan panjang sembarang. Mereka kadang dibutuhkan, karena angka regular tak bisa lebih dari <code>2<sup>53</sup></code> atau kurang dari <code>-2<sup>53</sup></code>. Karena bigint dipakai di sedikit area spesial, kita khususkan mereka bab spesial <info:bigint>.
9
+
=======
10
+
2. BigInt numbers, to represent integers of arbitrary length. They are sometimes needed, because a regular number can't safely exceed <code>2<sup>53</sup></code> or be less than <code>-2<sup>53</sup></code>. As bigints are used in few special areas, we devote them a special chapter <info:bigint>.
11
+
>>>>>>> 4d01fc20d4d82358e61518a31efe80dec9bb2602
8
12
9
13
Jadi di sini kita akan bahas angka regular. Ayo perluas pengetahuan kita tentang mereka.
Dengan kata lain, `"e"` kalikan angkanya dengan `1` dengan jumlah nol yang diberikan.
38
42
39
43
```js
40
-
1e3=1*1000// e3 means *1000
41
-
1.23e6=1.23*1000000// e6 means *1000000
44
+
1e3===1*1000;// e3 means *1000
45
+
1.23e6===1.23*1000000;// e6 means *1000000
42
46
```
43
47
44
48
Sekarang ayo tulis sesuatu lebih kecil. Katakan, 1 microsecond (sepersejuta second):
@@ -53,16 +57,28 @@ Sama seperti sebelumnya, memakai `"e"` bisa membantu. Jika kita ingin menghindar
53
57
let ms =1e-6; // enam nol di sebelah kiri dari 1
54
58
```
55
59
60
+
<<<<<<< HEAD
56
61
Jika kita hitung nol di `0.000001`, ada 6 dari mereka. Jadi alaminya `1e-6`.
62
+
=======
63
+
If we count the zeroes in `0.000001`, there are 6 of them. So naturally it's `1e-6`.
64
+
>>>>>>> 4d01fc20d4d82358e61518a31efe80dec9bb2602
57
65
58
66
Dengan kata lain, angka negatif setelah `"e"` artinya pembagian 1 dengan jumlah nol yang diberikan:
59
67
60
68
```js
69
+
<<<<<<<HEAD
61
70
// -3 membagi 1 dengan 3 nol
62
71
1e-3=1/1000 (=0.001)
63
72
64
73
// -6 membagi 1 dengan 6 nol
65
74
1.23e-6=1.23/1000000 (=0.00000123)
75
+
=======
76
+
// -3 divides by 1 with 3 zeroes
77
+
1e-3===1/1000; // 0.001
78
+
79
+
// -6 divides by 1 with 6 zeroes
80
+
1.23e-6===1.23/1000000; // 0.00000123
81
+
>>>>>>>4d01fc20d4d82358e61518a31efe80dec9bb2602
66
82
```
67
83
68
84
### Hex, angka binary dan octal
@@ -117,7 +133,12 @@ Tolong ingat bahwa dua dot di `123456..toString(36)` bukan typo. Jika kita mau m
117
133
118
134
Jika kita menaruh dot tunggal: `123456.toString(36)`, maka akan ada galat, karena syntax JavaScript berimplikasi bahwa bagian desimal setelah dot pertama. Dan jika kita menaruh satu dot lagi, maka JavaScript tahu bahwa bagian desimal kosong dan sekarang pergi ke metode.
119
135
136
+
<<<<<<< HEAD
120
137
Juga bisa menulis `(123456).toString(36)`.
138
+
=======
139
+
Also could write `(123456).toString(36)`.
140
+
141
+
>>>>>>> 4d01fc20d4d82358e61518a31efe80dec9bb2602
121
142
```
122
143
123
144
## Pembulatan
@@ -328,7 +349,11 @@ let num = +prompt("Enter a number", '');
328
349
alert( isFinite(num) );
329
350
```
330
351
352
+
<<<<<<<HEAD
331
353
Harap dicatat bahwa string kosong atau spasi-saja diperlakukan sebagai `0` dalam semua fungsi numerik termasuk` isFinite`.
354
+
=======
355
+
Please note that an empty or a space-only string is treated as `0`in all numeric functions including `isFinite`.
0 commit comments