Hordhac
function expressions iyo anonymous functions. Maqaalkan waxaan ku sharxi doonaa waxa ay yihiin labadan fikradood iyo sida loo isticmaalo.
Function Expressions and Anonymous Functions in JavaScript |
Waa Maxay Function Expression?
JavaScript, waxaad ku abuuri kartaa function qaabab kala duwan. Mid ka mid ah hababkaas waa function expression. Function expression waa marka aad abuurto function adigoo ku kaydinaya isbeddel (variable). Si ka duwan function declarations, function expressions uma baahna magac si ay u shaqeeyaan, inkasta oo aad magacaabi karto haddii aad rabto.
Tusaale ahaan:
javascript
const sayHello = function() {
console.log("Hello, World!");
};
sayHello(); // Waxay soo saari doontaa: Hello, World!
Halkan, waxaan abuurnay function ku kaydsan isbeddelka sayHello. Function-kan waxaa la fulin karaa markasta oo aad rabto, adoo wacaya magaca isbeddelka.
Anonymous Functions
Anonymous functions waa functions aan lahayn magac. Badanaa waxaa loo isticmaalaa xaaladaha sida function expressions ama functions la diraayo (callback functions). Anonymous function-ka waxaa la abuuraa iyadoo aan la siin wax magac ah marka la qeexayo.
Tusaale:
javascript
const greet = function() {
console.log("Greetings!");
};
greet(); // Waxay soo saari doontaa: Greetings!
Sida aad arki karto, function-ka aan magaca lahayn ayaa lagu kaydiyey isbeddelka greet, kadibna waxaa la wacayaa si ay shaqadeeda u fuliso.
Farqiga u Dhaxeeya Function Expressions iyo Function Declarations
Function declarations waa marka function si toos ah loogu abuuro koodkaaga iyadoo leh magac, halka function expressions lagu kaydiyo isbeddel. Mid ka mid ah farqiga ugu weyn waa hoisting.
Function declarations waxay ku hoist-yihiin dusha koodka, taas oo macnaheedu yahay inaad wici karto ka hor intaadan xitaa qeexin.
Tusaale:
javascriptgreet(); function greet() { console.log("Hello!"); }
Function expressions looma hoist-yo, taas oo macnaheedu yahay in aanad wici karin ka hor inta aanad abuuri.
Tusaale:
javascriptsayHello(); // Tani waxay keeni doontaa qalad (ReferenceError) const sayHello = function() { console.log("Hello!"); };
Isticmaalka Anonymous Functions
Anonymous functions waxaa loo isticmaalaa xaalado badan oo kala duwan. Mid ka mid ah meelaha ugu caansan ee loo isticmaalo waa marka aad dirayso callback functions. Tusaale ahaan, marka aad isticmaasho function sida setTimeout
, waxaad si toos ah ugu diri kartaa anonymous function ah.
Tusaale:
javascript
setTimeout(function() {
console.log("This message will appear after 3 seconds.");
}, 3000);
Halkan, anonymous function ayaa la diraa oo la wacayaa kadib marka saddex ilbiriqsi ay ka soo wareegaan.
Faa’iidooyinka Isticmaalka Function Expressions iyo Anonymous Functions
- Iswaafajin: Function expressions waxay kuu oggolaanayaan inaad si fudud ku kaydiso oo aad ula tacaasho functions sidii waxyaabo kale oo xog ah.
- Codsi ku Habboon: Anonymous functions waxay ku habboon yihiin marka aad u baahan tahay inaad sameyso hal-isticmaal oo keliya oo aan magac loo baahneyn.
- Callback Functions: Waxay ku habboon yihiin habka aad u diri karto functions si loogu fuliyo marka fal gaar ah uu dhaco.
Gunaanad
JavaScript function expressions iyo anonymous functions waa laba ka mid ah waxyaabaha muhiimka ah ee lagu sameeyo kood si dabacsan oo awood leh. Function expressions waxay kuu oggolaadaan inaad abuuri karto function-yada oo lagu kaydiyo isbeddellada, halka anonymous functions aad si toos ah ugu isticmaali karto meesha aad u baahan tahay inaad ku fuliso waxqabad gaaban. Fahamka sida iyo goorta loo isticmaalo labadaas waa tallaabo muhiim ah oo ku saabsan horumarinta JavaScript.