Pemrograman Beriorentasi Object Menggunakan Javascript (Part II)

  1. Menggunakan Module Pattern Object Literal

Object Literal digambarkan dalam kumpulan comma-separated name/value pairs yang diapit oleh curly braces ({}). Object literal tidak memerlukan instanisasi.

ar myObjectLiteral = {

variableKey: variableValue,

functionKey: function () {
// ...
}
};

var personModule = {

name: "nila",

// object literals can contain properties and methods.
// e.g we can define a further object for module configuration:
dob: {
place: "campalagian",
date: "10-10-1990"
},

// a very basic method
saySomething: function () {
console.log( "hello!!" );
},

// output a value based on the current configuration
showName: function () {
console.log( "my name is " + this.name );
},

// override the current configuration
updateDob: function( newDob ) {

if ( typeof newDob  === "object" ) {
this.dob = newDob;
console.log( this.dob.place);
}
}
};

// Outputs: hello!!
personModule.saySomething();

// Outputs: my name is nila
personModule.showName();

// Outputs: semarang
personModule.updateDob({
place: "semarang",
date: "10-10-1990"
});

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s