Function Declaration vs Function Expression

Function Declaration – bu funksiyani e’lon qilishdir.

E’lon qilingan funksiya keyinroq u chaqirilganda ishga tushirish uchun saqlab qo’yilgan bo’ladi.

O’zgaruvchilarni e’lon qilish var, let, yoki const kabi kalit so’zlar bilan boshlanganidek funksiya…


This content originally appeared on DEV Community and was authored by Dawrounn

Function Declaration - bu funksiyani e'lon qilishdir.

E'lon qilingan funksiya keyinroq u chaqirilganda ishga tushirish uchun saqlab qo'yilgan bo'ladi.

O'zgaruvchilarni e'lon qilish var, let, yoki const kabi kalit so'zlar bilan boshlanganidek funksiyani deklaratsiyasi ham function kalit so'zi bilan boshlanadi.

Masalan:

function test(){

return 4;
}
  • Bu yerda funksiya e'lon qilindi holos. Uni ishga tushirish uchun funksiya nomi test()'dan foydalanib funksiyani chaqirishimiz kerak.

Function Expression

Javascriptda funksiya ifoda yordamida ham aniqlanishi mumkin. Funksiya ifodasi(expression) o'zgaruvchiga saqlanishi ham mumkin:
var x = function(a,b) { return a*b};

Funksiya ifodasi o'zgaruvchiga saqlangandan so'ng aynan shu o'zgaruvchidan funksiya sifatida foydalanish mumkin. O'zgaruvchiga saqlanadigan funksiyalarda funksiya nomini ko'rsatish shart emas. Ular doimo o'zgaruvchi nomi orqali chaqiriladi va ishga tushiriladi.

Function Expression:

alert(test()); // ERROR! test topilmadi
var test = function() { return 5; }

Function Declaration:

alert(test()); // Alert 5. dekleratsiyash birinchi amalga oshiriladi
function test() { return 5; }
  • Funksiya deklaratsiyasi boshqa har qanday kod ishga tushishidan oldin bajariladi, funnksiya ifodasi(expression) esa interpreter shu qatorga yetib kelganda bajariladi.

  • var kalit so'zidek funksiya deklaratsiyalari kodning eng yuqorisiga avtomatik ko'tariladi(hoisted). Funksiya ifoda(expression) lar esa ko'tarilmaydi.

Function expression'ning afzalliklari:

Funktsiya ifodalari funksiya deklaratsiyasidan ko'ra foydaliroq bo'lishining bir necha xil usullari mavjud:

  • Closure sifatida;

  • Boshqa funksiyalarga argument sifatida;

  • Darhol chaqiriladigan funksiya ifodalari (IIFE) sifatida;


This content originally appeared on DEV Community and was authored by Dawrounn


Print Share Comment Cite Upload Translate Updates
APA

Dawrounn | Sciencx (2022-04-22T16:58:46+00:00) Function Declaration vs Function Expression. Retrieved from https://www.scien.cx/2022/04/22/function-declaration-vs-function-expression/

MLA
" » Function Declaration vs Function Expression." Dawrounn | Sciencx - Friday April 22, 2022, https://www.scien.cx/2022/04/22/function-declaration-vs-function-expression/
HARVARD
Dawrounn | Sciencx Friday April 22, 2022 » Function Declaration vs Function Expression., viewed ,<https://www.scien.cx/2022/04/22/function-declaration-vs-function-expression/>
VANCOUVER
Dawrounn | Sciencx - » Function Declaration vs Function Expression. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/22/function-declaration-vs-function-expression/
CHICAGO
" » Function Declaration vs Function Expression." Dawrounn | Sciencx - Accessed . https://www.scien.cx/2022/04/22/function-declaration-vs-function-expression/
IEEE
" » Function Declaration vs Function Expression." Dawrounn | Sciencx [Online]. Available: https://www.scien.cx/2022/04/22/function-declaration-vs-function-expression/. [Accessed: ]
rf:citation
» Function Declaration vs Function Expression | Dawrounn | Sciencx | https://www.scien.cx/2022/04/22/function-declaration-vs-function-expression/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.