This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns sample.core | |
(:require [cljsjs.react] | |
[reagent.core :as reagent :refer [atom]])) | |
(defn text-editor | |
"Text editor for static blog." | |
[] | |
(let [grid (aget js/ReactBootstrap "Grid") | |
row (aget js/ReactBootstrap "Row") | |
col (aget js/ReactBootstrap "Col")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Contoh higher-order function | |
function salam(time) { | |
return function(userName) { | |
return time + " " + userName; | |
} | |
} | |
// Jika dipanggil langsung | |
salam("Selamat pagi")("Asep"); // Hasilnya: "Selamat pagi Asep" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sampleData = [1, 2, 3, 4, 5]; | |
/* | |
Buat fungsi yang mengalikan setiap elemen dengan angka 2. | |
Hasil yang diharapkan [2, 4, 6, 8, 10] | |
*/ | |
// Menggunakan perulangan for | |
function denganPerulangan(data) { | |
var expectedResult = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sampleData = [1, 2, 3, 4, 5]; | |
/* | |
Buat fungsi yang menghasilkan Array yang isinya hanya bilangan genap. | |
Hasil yang diharapkan [2, 4] | |
*/ | |
// Menggunakan perulangan for | |
function denganPerulangan(data) { | |
var expectedResult = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sampleData = [1, 2, 3, 4, 5]; | |
/* | |
Buat fungsi yang mengakumulasi keseluruhan elemen dalam Array. | |
Hasil yang diharapkan 15 | |
*/ | |
// Menggunakan perulangan for | |
function denganPerulangan(data) { | |
var result = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
/** | |
Tiga operasi menghapus file di bawah ini, walaupun disusun berurutan | |
tetapi belum tentu selesainya berurutan dari operasi 1 sampai operasi 3. | |
*/ | |
// Operasi 1 | |
fs.unlink('/file-one', function(err) { | |
console.log('file one is deleted'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fungsi yang tidak membutuhkan callback | |
function sum(a, b) { | |
return a + b; | |
} | |
// isi dari variabel result adalah 6 | |
var result = sum(1, 5); | |
/** | |
Di bawah ini adalah contoh function yang membutuhkan callback. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Di bawah ini adalah penjelasan bagaimana asynchronous bekerja. | |
Saya ambil contoh jQuery.get() dan sebuah operasi penjumlahan sederhana. | |
Ini adalah function dari pustaka jQuery untuk melakukan pemanggilan data menggunakan ajax. | |
*/ | |
// Program akan mengeksekusi baris 8 | |
jQuery.get('http://example.com/data.json', iniCallback); | |
// Baris ke 14 sampai ke 15 baru akan tereksekusi hanya saat jQuery.get() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Pemrogram berusaha menyimpan nilai dari callback ke dalam variabel | |
yang berada di luar scope callback. | |
*/ | |
var iniVariabelGlobal; | |
jQuery.get('http://example.com/data.json', function(data) { | |
iniVariabelGlobal = data; // pemrogram berusaha menyimpan data ke dalam global variable | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Misalnya kita memiliki fungsi yang baru dapat dijalankan setelah ada nilai dari jQuery.get(), | |
jangan gunakan global variable seperti sebelumnya. Buatlah sebuah fungsi baru di luar asynchronous function | |
yang dapat dipanggil setelah jQuery.get() selesai. | |
*/ | |
jQuery.get('http://example.com/data.json', function(data) { | |
doSomething(data); // lempar data dari server ke dalam sebuah fungsi | |
}); |
OlderNewer