Secara sederhana, prototype adalaah cara muda buat nambain method atau parameter baru dalam suatu fungsi. Mmmm...misalkan kita ada fungsi kyk gini :
1
2
3
4
5
6
7
8
9
10
11
12
13
| function Akatsuki()
{
this.name = "";
this.power = 100;
this.givePower = function givePower(memberTarget)
{
memberTarget.power += 1;
console.log(this.name+" has given 1 power to "+memberTarget.name);
}
}
var Hisoka = new Akatsuki();
var Itachi = new Akatsuki();
|
Naaah... disini kita ada 2 var Hisoka dan Itachi yang masing2 berupa Akatsuki, kalo misalkan Hisoka ngasih 1 power ke Itachi berarti :
1
2
3
4
5
6
7
8
| Hisoka.name = "Hisoka";
Itachi.name = "Itachi";
Hisoka.givePower(Itachi);
//Cek masing-masing power
console.log("Hisoka's Power : "+Hisoka.power);
console.log("Itachi's Power : "+Itachi.power);
|
Hasilnya :
Naaah... klu udah smpe sini, kita misalkan pengen nambah sesuatu ke dalam fungsi Akatsuki, itu bisa pake prototype, misalkan kita pengen nambain takePower, jadinya kyk gini :
1
2
3
4
5
6
7
| //pake prototype buat nambain variabel baru dalam Akatsuki
Akatsuki.prototype.takePower = function takePower(memberTarget)
{
memberTarget.power -= 7;
this.power += 7;
console.log(this.name +" has taken power from "+ memberTarget.name);
}
|
Kita udah nambain takePower ke Akatsuki berarti sekarang Hisoka udah bisa pake takepower-nya, contoh kyk gini :
1
2
3
4
| Hisoka.takePower(Itachi);
//Cek masing-masing power
console.log("Hisoka's Power : "+Hisoka.power);
console.log("Itachi's Power : "+Itachi.power);
|
Hasilnya :
Naaah... kliataan kaan... Hisoka nambah 7, Itachi kurang 7. Dari sini juga jelas penggunaan prototype kekna... :D mmm.... coba satu lagi kyk gini :
1
2
3
4
| Akatsuki.prototype.jutsu = "Secret... :D";
//cek lagi...
console.log("Hisoka's Jutsu : ",Hisoka.jutsu);
console.log("Itachi's Jutsu : ",Itachi.jutsu);
|
Hasilnya :
Siiips... ckup demikin... :D ....
Untuk source code lengkapnya seperti ini :
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
| function Akatsuki()
{
this.name = "";
this.power = 100;
this.givePower = function givePower(memberTarget)
{
memberTarget.power += 1;
console.log(this.name+" has given 1 power to "+memberTarget.name);
}
}
var Hisoka = new Akatsuki();
var Itachi = new Akatsuki();
Hisoka.name = "Hisoka";
Itachi.name = "Itachi";
Hisoka.givePower(Itachi);
//Cek masing-masing power
console.log("Hisoka's Power : "+Hisoka.power);
console.log("Itachi's Power : "+Itachi.power);
//pake prototype buat nambain variabel baru dalam Akatsuki
Akatsuki.prototype.takePower = function takePower(memberTarget)
{
memberTarget.power -= 7;
this.power += 7;
console.log(this.name +" has taken power from "+ memberTarget.name);
}
Hisoka.takePower(Itachi);
//Cek masing-masing power
console.log("Hisoka's Power : "+Hisoka.power);
console.log("Itachi's Power : "+Itachi.power);
Akatsuki.prototype.jutsu = "Secret... :D";
//cek lagi...
console.log("Hisoka's Jutsu : ",Hisoka.jutsu);
console.log("Itachi's Jutsu : ",Itachi.jutsu);
|
No comments:
Post a Comment