⊗jsPmMCLb 505 of 505 menu

Biblioteke deur sluitings in JavaScript

Dikwels word biblioteke in JavaScript geskep, wat versamelings funksies is vir gebruik deur ander programmeerders.

Sulke biblioteke word gewoonlik in modules toegedraai deur sluitings. Dit word gedoen sodat daar so min as moontlik funksies in die buitewêreld verskyn wanneer die biblioteek gekoppel word.

Oor die algemeen probeer elke biblioteek slegs een veranderlike in die buitewêreld skep - 'n voorwerp met die funksies van die biblioteek. Terselfdertyd is sommige funksies binne die biblioteek se kode hooffunksies, en ander is hulpfunksies. Dit is duidelik dat ons slegs die nodige funksies na die buitewêreld wil uitvoer, sonder om die uitgevoerde voorwerp te verstop met hulpfunksies.

Kom ons kyk na 'n voorbeeld. Gestel ons het die volgende stel funksies wat ons graag in 'n biblioteek wil omskep:

function square(num) { return num ** 2; } function cube(num) { return num ** 3; } function avg(arr) { return sum(arr, 1) / arr.length; } function digitsSum(num) { return sum(String(num).split('')); } // hulpfunksie function sum(arr) { let res = 0; for (let elem of arr) { res += +elem; } return res; }

Kom ons vorm ons funksies as 'n module:

;(function() { function square(num) { return num ** 2; } function cube(num) { return num ** 3; } function avg(arr) { return sum(arr, 1) / arr.length; } function digitsSum(num) { return sum(String(num).split('')); } // hulpfunksie function sum(arr) { let res = 0; for (let elem of arr) { res += +elem; } return res; } })();

En nou voer ons alle funksies uit, behalwe die hulpfunksie:

;(function() { function square(num) { return num ** 2; } function cube(num) { return num ** 3; } function avg(arr) { return sum(arr, 1) / arr.length; } function digitsSum(num) { return sum(String(num).split('')); } // hulpfunksie function sum(arr) { let res = 0; for (let elem of arr) { res += +elem; } return res; } window.math = {square, cube, avg, digitsSum}; })();

Gestel ons het 'n HTML-bladsy index.html:

<html> <head> <script> </script> </head> </html>

Laat ons ons biblioteek daaraan koppel:

<html> <head> <script src="math.js"></script> <script> </script> </head> </html>

Laat ons die funksies van ons biblioteek gebruik:

<html> <head> <script src="math.js"></script> <script> alert(math.avg([1, 2, 3]) + math.square()); </script> </head> </html>

Die volgende kode word gegee:

function avg1(arr) { return sum(arr, 1) / arr.length; } function avg2(arr) { return sum(arr, 2) / arr.length; } function avg3(arr) { return sum(arr, 3) / arr.length; } // hulpfunksie function sum(arr, pow) { let res = 0; for (let elem of arr) { res += elem ** pow; } return res; }

Vorm hierdie kode om as 'n module. Voer alle funksies na buite uit, behalwe die hulpfunksie.

Bestudeer die biblioteek underscore. Skep jou eie soortgelyke biblioteek deur 5-10 funksies van die oorspronklike biblioteek te herhaal.

Afrikaans
AzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEnglishEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
Ons gebruik koekies vir die werking van die webwerf, ontleding en personalisering. Die verwerking van data geskied volgens die Privaatheidsbeleid.
aanvaar alles instel verwerp