Anda di halaman 1dari 31

Javascipt V

1.memisahkan file

File : script.js

// Pindahkan pendefinisian class Animal ke animal.js dan hapus code dibawah

// Pindahkan pendefinisian class Dog ke dog.js dan hapus code dibawah

// Code dibawah jangan diganggu karena bukan bagian dari class Animal ataupun Dog

const dog = new Dog("Leo", 4, "Chihuahua");

dog.info();

file : animal.js

// Salin definisi class Animal ke bagian dibawah ini

class Animal {

constructor(name, age) {

this.name = name;

this.age = age;

greet() {

console.log("Halo");

info() {

this.greet();
console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

File : dog.js

// Salin definisi class Dog kebagian dibawah ini

class Dog extends Animal {

constructor(name, age, breed) {

super(name, age);

this.breed = breed;

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya adalah seekor ${this.breed}`);

console.log(`Saya berusia ${this.age} tahun`);

const humanAge = this.getHumanAge();

console.log(`Saya berusia ${humanAge} tahun dalam umur manusia`);

getHumanAge() {

return this.age * 7;

}
}

2. Memisahkan File (2)

File:script

// Import class Dog dibawah baris ini

import Dog from "./dog";

const dog = new Dog("Leo", 4, "Chihuahua");

dog.info();\

file:animal.js

class Animal {

constructor(name, age) {

this.name = name;

this.age = age;

greet() {

console.log("Halo");

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

}
}

// Export class Animal dibawah baris ini

export default Animal;

File:dog.js

// Import class Animal dibawah baris ini

import Animal from "./animal";

class Dog extends Animal {

constructor(name, age, breed) {

super(name, age);

this.breed = breed;

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya adalah seekor ${this.breed}`);

console.log(`Saya berusia ${this.age} tahun`);

const humanAge = this.getHumanAge();

console.log(`Saya berusia ${humanAge} tahun dalam umur manusia`);

getHumanAge() {

return this.age * 7;
}

// Export class Dog dibawah baris ini

export default Dog;

3. Meng-export Nilai
File:script.js

// Hapus code dibawah

// Hapus code diatas

// Import constant dog

import dog from "./dogData";

dog.info();

File:dogData.js

// Buat agar constant dog dapat diakses dari file berikut

import Dog from "./dog";

const dog = new Dog("Leo", 4, "Chihuahua");

// Export constant dog

export default dog;

File:animal.js
class Animal {

constructor(name, age) {

this.name = name;

this.age = age;

greet() {

console.log("Halo");

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

export default Animal;

File:dog.js

// Import class Animal dibawah baris ini

import Animal from "./animal";

class Dog extends Animal {

constructor(name, age, breed) {

super(name, age);
this.breed = breed;

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya adalah seekor ${this.breed}`);

console.log(`Saya berusia ${this.age} tahun`);

const humanAge = this.getHumanAge();

console.log(`Saya berusia ${humanAge} tahun dalam umur manusia`);

getHumanAge() {

return this.age * 7;

// Export class Dog dibawah baris ini

export default Dog;

4.Export Bernama
File:script.js

// Tulis ulang code dibawah and import constant dog1 dan dog2

import { dog1, dog2 } from "./dogData";

// Salin code di jendela instruksi dan tulis ulang constant dog1 dan dog2 agar dapat dicetak
console.log("---------");

dog1.info();

console.log("---------");

dog2.info();

File:dogData.js

// Tulis ulang code dibawah and import constant dog1 dan dog2

import { dog1, dog2 } from "./dogData";

// Salin code di jendela instruksi dan tulis ulang constant dog1 dan dog2 agar dapat dicetak

console.log("---------");

dog1.info();

console.log("---------");

dog2.info();

File:animal.js

class Animal {

constructor(name, age) {

this.name = name;

this.age = age;

greet() {

console.log("Halo");

info() {
this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

export default Animal;

File:dog.js

import Animal from "./animal";

class Dog extends Animal {

constructor(name, age, breed) {

super(name, age);

this.breed = breed;

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya adalah seekor ${this.breed}`);

console.log(`Saya berusia ${this.age} tahun`);

const humanAge = this.getHumanAge();

console.log(`Saya berusia ${humanAge} tahun dalam umur manusia`);

}
getHumanAge() {

return this.age * 7;

export default Dog;

5.Jalur Relatif
File:script.js

// Ubah jalur relatif "./dogData"

import { dog1, dog2 } from "./data/dogData";

console.log("---------");

dog1.info();

console.log("---------");

dog2.info();

File:dogData.js

// Ubah jalur relatif "./dog"

import Dog from "../class/dog";

const dog1 = new Dog("Leo", 4, "Chihuahua");

const dog2 = new Dog("Ben", 2, "Poodle");

export { dog1, dog2 };

6.Paket (1)

File:dog.js
// Import paket chalk

import chalk from "chalk";

import Animal from "./animal";

class Dog extends Animal {

constructor(name, age, breed) {

super(name, age);

this.breed = breed;

info() {

const humanAge = this.getHumanAge();

this.greet();

// Tulis ulang konten dari console.log menggunakan chalk

console.log(chalk.yellow(`Nama saya adalah ${this.name}`));

// Tulis ulang konten dari console.log menggunakan chalk

console.log(chalk.bgCyan(`Saya adalah seekor ${this.breed}`));

console.log(`Saya berusia ${this.age} tahun`);

console.log(`Saya berusia ${humanAge} tahun dalam umur manusia`);

}
getHumanAge() {

return this.age * 7;

File:script.js

import { dog1, dog2 } from "./data/dogData";

console.log("---------");

dog1.info();

console.log("---------");

dog2.info();

File:dogData.js

import Dog from "../class/dog";

const dog1 = new Dog("Leo", 4, "Chihuahua");

const dog2 = new Dog("Ben", 2, "Poodle");

export { dog1, dog2 };

7.Paket (2)

FIile:dogData.js

// Import readline-sync

import readlineSync from "readline-sync";

import Dog from "../class/dog";


const dog1 = new Dog("Leo", 4, "Chihuahua");

// Tulis ulang dengan menggunakan readlineSync.question

const name = readlineSync.question("Ketik nama Anda: ");

// Tulis ulang dengan menggunakan readlineSync.questionInt

const age = readlineSync.questionInt("Ketik usia Anda: ");

// Tulis ulang dengan menggunakan readlineSync.question

const breed = readlineSync.question("Ketik ras Anda: ");

const dog2 = new Dog(name, age, breed);

export { dog1, dog2 };

File:dog.js

import chalk from "chalk";

import Animal from "./animal";

class Dog extends Animal {

constructor(name, age, breed) {

super(name, age);

this.breed = breed;

}
info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya adalah seekor ${this.breed}`);

console.log(`Saya berusia ${this.age} tahun`);

const humanAge = this.getHumanAge();

console.log(`Saya berusia ${humanAge} tahun dalam umur manusia`);

getHumanAge() {

return this.age * 7;

File:script.js

import { dog1, dog2 } from "./data/dogData";

console.log("---------");

dog1.info();

console.log("---------");

dog2.info();

Javascript vi
1.Reviuw object

File:script.js

// Definisikan constant animal

const animal = {

name: "Leo",

age: 3,

greet: () => {

console.log("Halo");

};

// Print property name milik constant animal

console.log(animal.name);

// Panggil greet method property milik animal

animal.greet();

2. Apa yang dimaksud dengan Class?

File:script.js

// Definisikan class Animal

class Animal {

3. Membuat Instanc

File:scipt

class Animal {

}
// Tetapkan instance class Animal ke constant animal

const animal = new Animal();

// Tampilkan nilai milik constant animal

console.log(animal);

4.Construct (1)
File:script.js

class Animal {

// Tambahkan constructor

constructor() {

console.log("Membuat instance baru");

const animal = new Animal();

5. Constructor (2)

File:script.js

class Animal {

constructor() {

// Tetapkan nilai string「Leo」ke property name

this.name = "Leo";

// Tetapkan nilai「3」ke property age

this.age = 3;
}

const animal = new Animal();

// Print「Nama: ____」

console.log(`Nama: ${animal.name}`);

// Print「Usia: __」

console.log(`Usia: ${animal.age}`);

6. Constructor (3)
File:script.js

class Animal {

// Tambahkan argument「name」dan「age」

constructor(name, age) {
7

// Gantikan nilai string "Leo" dengan nilai milik argument name

this.name = name; class Animal {

// Tambahkan argument「name」dan「age」

constructor(name, age) {

// Gantikan nilai string "Leo" dengan nilai milik argument name

this.name = name;

// Gantikan「3」dengan nilai milik argument age

this.age = age;

// Teruskan argument「"Mocha"」「8」ke constant animal dibawah

const animal = new Animal("Mocha", 8);

console.log(`Nama: ${animal.name}`);

console.log(`Usia: ${animal.age}`);

// Gantikan「3」dengan nilai milik argument age

this.age = age;

// Teruskan argument「"Mocha"」「8」ke constant animal dibawah


const animal = new Animal("Mocha", 8);

console.log(`Nama: ${animal.name}`);
console.log(`Usia: ${animal.age}`);

7.METHOD (1)

File: class Animal {

constructor(name, age) {

this.name = name;

this.age = age;

// Tambahkan method greet

greet() {

console.log("Halo");

const animal = new Animal("Leo", 3);

console.log(`Nama: ${animal.name}`);

console.log(`Usia: ${animal.age}`);

// Panggil method greet milik animal instance

animal.greet();
8. Method (2
File:script.js

class Animal {

constructor(nama, usia) {

this.name = nama;

this.age = usia;

greet() {

console.log("Halo");

// Tambahkan method info

info() {

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

const animal = new Animal("Leo", 3);

animal.greet();

9. Memanggil Method di dalam Method


File:script.js

class Animal {

constructor(name, age) {

this.name = name;
this.age = age;

greet() {

console.log("Halo");

info() {

// Panggil method greet

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berumur ${this.age} tahun`);

const animal = new Animal("Leo", 3);

// Hapus baris dibawah

animal.info();

Inheritance Class
10. Apa yang dimaksud dengan Inheritance?

File:script.js

class Animal {

constructor(name, age) {
this.name = name;

this.age = age;

greet() {

console.log("Halo");

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berumur ${this.age} tahun`);

// Definisikan class Dog agar dapat menerima warisan dari class Animal

class Dog extends Animal {

11.Menggunakan inheritance class

File:script.js

class Animal {

constructor(name, age) {

this.name = name;

this.age = age;

}
greet() {

console.log("Halo");

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

class Dog extends Animal {

// Terapkan instance class Dog ke constant dog

const dog = new Dog("Leo", 4);

// Panggil method info dari constant dog

dog.info();

11.Menambah Method
File:script.js

class Animal {

constructor(name, age) {

this.name = name;
this.age = age;

greet() {

console.log("Halo");

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

class Dog extends Animal {

// Tambahkan method getHumanAge

getHumanAge() {

return this.age * 7;

const dog = new Dog("Leo", 4);

dog.info();

// Panggil method getHumanAge milik instance dog


const humanAge = dog.getHumanAge();

// Print 「Saya berusia __ tahun dalam umur manusia」

console.log(`Saya berusia ${humanAge} tahun dalam umur manusia`);

13. Overriding (1)


File:script.js

class Animal {

constructor(name, age) {

this.name = name;

this.age = age;

greet() {

console.log("Halo");

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

class Dog extends Animal {

// Tambahkan method info


info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

const humanAge = this.getHumanAge();

console.log(`Saya berusia ${humanAge} tahun dalam umur manusia`);

getHumanAge() {

return this.age * 7;

const dog = new Dog("Leo", 4);

dog.info();

14. Overriding (2)


File:script.j

class Animal {

constructor(name, age) {

this.name = name;

this.age = age;

greet() {
console.log("Halo");

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

console.log(`Saya berusia ${this.age} tahun`);

class Dog extends Animal {

// Tambahkan constructor

constructor(name, age, breed) {

super(name, age);

this.breed = breed;

info() {

this.greet();

console.log(`Nama saya adalah ${this.name}`);

// Print 「Saya adalah seekor ____」

console.log(`Saya adalah seekor ${this.breed}`);

console.log(`Saya berusia ${this.age} tahun`);

const humanAge = this.getHumanAge();


console.log(`Saya berusia ${humanAge} tahun dalam umur manusia`);

getHumanAge() {

return this.age * 7;

// Tetapkan "Chihuahua" sebagai nilai argument

const dog = new Dog("Leo", 4, "Chihuahua");

dog.info();

Javascript VII

Mempelajari tentang Callback Function


1. Mempelajari tentang Callback Function

File:script.js

const printKen = () => {

console.log("Ninja Ken");

};
const printMaster = () => {

console.log("Guru Domba");

};

const call = (callback) => {

console.log("Memanggil function callback.");

callback();

};

// Ketik ulang argument ini sebagai printMaster dan konfirmasikan output-nya

call(printMaster);

2. Apa yang Dimaksud dengan Callback Function?

File:script.js

const printKen = () => {

console.log("Ninja Ken");

};

// Tambahkan parameter callback ke function call

const call = (callback) => {

console.log("Memanggil function callback.");

// Panggil callback function

callback();

};
// Teruskan printKen sebagai argument dan jalankan function call

call(printKen);

3. Secara Langsung Mendeklarasikan Callback Function

File:script.js

const printKen = () => {

console.log("Ninja Ken");

};

const call = (callback) => {

console.log("Memanggil callback function.");

callback();

};

call(printKen);

// Deklarasikan function didalam argument dan teruskan

call(() => {

console.log("Guru Domba");

});

4. Argument Callback Function

File:script.js

const call = (callback) => {

callback("Ninja Ken", 14);

};
// Tambahkan sebuah function yang menerima dua argument didalam argument call

call((name, age) => {

console.log(`${name} berusia ${age} tahun.`);

});

Anda mungkin juga menyukai