Javascript: prototype to create new function

August 12, 2013 at 1:05 am Leave a comment

Yap, langsung dicoba baca dan implement di browser saja yah. Aku pakai Firebug.

//Menggunakan fungsi floor secara langsung
console.log(Math['floor'](3.5)); //hasilnya 3

//Membuat fungsi interger() dengan prototype
Function.prototype.integer = Math['floor']();

//begini implementasinya
console.log((3.5).integer()); //hasilnya 3

dengan prototype kita bisa buat fungsi baru dari kelas “Function” yang isinya itu fungsi turunan Math, pada contoh diatas kita coba dengan Math.floor() / Math[‘floor’]().

Contoh lain penerapan protoype:

//create a root class, harus variable yang memiliki function
//istilahnya function literal
var Numbering = function(){};

//create a function inherit from Numbering
Numbering.prototype.kali = function(number){
return number*number;
};

//Create an object that implement of "Numbering"
var myNumbering = new Numbering();

//test new function
console.log(myNumbering.kali(8));// hasilnya 64
//create a root class, harus variable yang memiliki function
//istilahnya function literal
var Numbering = function(){};

//create a function inherit from Numbering
Numbering.prototype.kali = function(number){
return number*number;
};

//Create an object that implement of "Numbering"
var myNumbering = new Numbering();

//test new function
console.log(myNumbering.kali(8));// hasilnya 64
//Menggunakan fungsi floor secara langsung
console.log(Math['floor'](3.5)); //hasilnya 3

//Membuat fungsi interger() dengan prototype
Function.prototype.integer = Math['floor']();

//begini implementasinya
console.log((3.5).integer()); //hasilnya 3//Menggunakan fungsi floor secara langsung
console.log(Math['floor'](3.5)); //hasilnya 3

//Membuat fungsi interger() dengan prototype
Function.prototype.integer = Math['floor']();

//begini implementasinya
console.log((3.5).integer()); //hasilnya 3

Entry filed under: My self. Tags: , , , .

Invisible != Unavailiable Back to Old Story?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Because I Learn CI and Jquery

http://api.jquery.com

http://www.grocerycrud.com

http://Zedwood.com

Twitter Updates

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

My FB page


%d bloggers like this: