Cara menggunakan does javascript support iife?

Apa Itu IIFE ?

Awal Saya berkenalan dengan kata IFFE, itu pun disebabkan munculnya rasa kepo setelah mengutak-atik algoritma milik jQuery. Banyak sekali hal baru yang Saya temui, terutama cara memanggil funsi yang tidak biasa (well aneh). “Kok bisa gini? Kok gak error ya?”, pertanyaan pertanyaan itu kemudian muncul. Berbekal keingintahuan yang teramat sangat, kemudian Saya mencari-cari tau. Tentunya dengan bantuan si “Mbah Google” yang serba tau dan whoilaa, Saya dipertemukan dengan IIFE. Well, secara teknis, ke-kepoan Saya kali ini pun berujung “Indah”.

Saat mencari artikel tentang IIFE, tentunya Kamu akan menemukan beragam definisi tentangnya. Mulai dari yang sederhana sampai yang ribet, intinya semua sama. So, apa itu IIFE?

(Versi Saya) IIFE merupakan cara pemanggilan fungsi di Javascript tanpa mendefinisikan fungsi itu sebelumnya.
Atau dapat juga diartikan: “Fungsi yang memanggil dirinya sendiri”. Mm, mulai pusing? hhe. Nah daripada pusing, yuk simak contoh IIFE berikut ini.

Picture 1 : Contoh IIFE

Cara menggunakan does javascript support iife?


Kegunaan IIFE

IIFE biasa digunakan untuk mengamankan variable di dalamnya sehingga menjadi private. Mengapa bisa? Kalau kalian familiar dengan pemrograman khususnya javascript, kalian tau bahwa setiap variable yang didefinisikan di dalam scope “{}” hanya dapat diakses dari dalam. Misal:

function example(){
  var a = 'Hi, Im a!'
  document.write(a)
}

example()

Hasilnya: Hi, Im a!

function example(){
  var a = 'Hi, Im a!'
}

example()
document.write(a)

Hasilnya: undefined


Membuat Framework dengan IIFE

IFEE biasa digunakan oleh framework seperti jQuery, Bootstrap, dll. So, let’s try it out!

1. Langkah pertama buatlah IIFE scope terlebih dahulu

(function(){ /*your code goes here*/ }())

2. Definisikan w sebagai parameter, dan masukkan window sebagai Argument. Nantinya “window” akan dikenal di dalam scope sebagai “w”.

(function(w){
  /*your code goes here*/ 
}(window))

3. Definisikan sebuah variable sebagai object yang nantinya akan menjadi nama framework-mu.

(function(w){

  var framework = {} 

}(window))

4. Definisikan method di dalamnya.

(function(w){
  
  var framework = {}

  framework.version = '1.0.0'
  framework.build_date = '20 Juni 2015'
  framework.programmer = 'Mias Marthinus'

  framework.detail = function(){
    alert( 
      framework.version + 
      ' - ' +
      framework.build_date +
      ' - ' +
      framework.programmer 
    )
  }

  /*add another method here*/

}(window))

5. Terakhir masukkan framework ke dalam window.

(function(w){
  
  var framework = {}

  framework.version = '1.0.0'
  framework.build_date = '20 Juni 2015'
  framework.programmer = 'Mias Marthinus'

  framework.detail = function(){
    alert( 
      framework.version + 
      ' - ' +
      framework.build_date +
      ' - ' +
      framework.programmer 
    )
  }

  /*add another method here*/
  
  w.framework = framework
}(window))

6. Whoilaa, framework milikmu sendiri sudah selesai dan dapat dipanggil kapanpun.

framework.detail()

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Life Counter

Hey there, i decided to make another simple tutorial but now for beginners and will explain how to make health counter.

Starting

Ok let's do it fast just create new empty project. Double click on your white screen and chose "Tiled background" now one window opened you can create your own background in photoshop or in construct too but for this time i added my work here :)

Cara menggunakan does javascript support iife?

Ok now we have background but change position of it to 0, 0 and resize it fit to your layout.

Sprites

Let's make few sprites first make your player sprite and sprite for your monster or anything what will decrease your health. You will be able to download my sprites below.

Cara menggunakan does javascript support iife?

Cara menggunakan does javascript support iife?

Now place it on your layout don't forget place it inside. Sprites are done let's jump in to behaviours.

Behaviours

Behaviours are interesting and why? Because behaviours making all functions for your sprite or objects. It's simple just chose your player sprite and left click on it and add behaviours. We have to add 8-Directions behaviour for player sprite so lets do it. For another sprites you dont need to add any behaviours at this time.

Layers

Layers are something like positions on your screen you can have top layers or bottom layers. Layers placed on top of layout are primary so you will be able to see it at any time but bottom layers like background can be overlapped with another like HUD or main. Stop with explaining and let's start with work.

Just on right side chose layers menu and add 2 more layers. Change their names to background ( bg if you want ), main and HUD. Be sure your background layer is at bottom and HUD and top so main will be at middle. Now click on your background sprite and on left side panel at Common chose layer to layer background and lock it on your right side other textures move to main layer.

Text

You have to add few more things to your main screen and its text box of course. So double click on your screen and chose text, do it 3 times. Change their names to HP, TotalHP, HPCOUNT (i'm using this names you can chose your own of course ) so you have 3 text boxes and for this part its done.

Event Sheet

Right click on your event sheet screen and add Global variable and name it "CurrentHP" now let's make substract process and set it to max HP as you'd like for me its 10 so number will be 10. Add your first condition Player > On collision with another object > Object is substractHP ( small red ball with my textures ). Now action for this condition is "System > Substract from CurrentHP 1" So each time when you hit red ball you will lose one health it's called "Substract" and now System > Every tick and action Text "HP" Set text HP. So every 10ns ( nano seconds ) will change your text to your current HP don't worry about lags this will never be able to make lags. Now let's place your text box. First on left side place text box called "HP" and right from this "HP" box place HPCOUNT box and in bottom of HPCOUNT text place text TotalHP and now let's continue to event sheet. Add another condition System > On start of layout and action Text box "HP" set text to "HP" and text box TotalHP set to "10" right now only start debugging and here you are :)) Thanks for reading my short tutorial i hope it was useful and if you want to support me just check my profile you can see there more tutorials:

Link with Ads

Normal Link