From cb9e359a51c3249d8f5157db69d43fd413ddeda6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:45:12 +0100 Subject: unslug ca: move --- .../reference/operators/function/index.html | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 files/ca/web/javascript/reference/operators/function/index.html (limited to 'files/ca/web/javascript/reference/operators/function/index.html') diff --git a/files/ca/web/javascript/reference/operators/function/index.html b/files/ca/web/javascript/reference/operators/function/index.html new file mode 100644 index 0000000000..0908f591b6 --- /dev/null +++ b/files/ca/web/javascript/reference/operators/function/index.html @@ -0,0 +1,146 @@ +--- +title: function expression +slug: Web/JavaScript/Referencia/Operadors/function +translation_of: Web/JavaScript/Reference/Operators/function +--- +
{{jsSidebar("Operators")}}
+ +

La paraula clau function es pot utilitzar per definir una funció dins d'una expressió.

+ +

Sintaxi

+ +
function [nom]([paràm1[, paràm2[, ..., paràmN]]]) {
+   sentències
+}
+ +

Paràmetres

+ +
+
nom
+
El nom de la funció. Es pot ometre, i en aquest cas la funció seria anònima. El nom és només local pel cos de la funció.
+
paràmN
+
El nom d'un argument que es passa a la funció.
+
sentències
+
Les sentències que constitueixen el cos de la funció.
+
+ +

Descripció

+ +

Una expressió d'una funció és molt semblant i té gairebé la mateixa sintaxi que una sentència d'una funció (function sentència d'una funció per més detalls). La principal diferència entre l'expressió d'una funció i una sentèndia d'una expressió és el nom de la functió, el qual es pot ometre en expressions de funcions per tal de crear funcions anònimes. Una expressió d'una funció es pot utilitzar com a un IIFE (Immediately Invoked Function Expression) que s'executa un cop s'ha definit. Vegeu també el capítol sobre funcions per més informació.

+ +

Exemples

+ +

L'exemple següent defineix una funció sense nom i l'assigna a x. La funció retorna el quadrat del seu argument:

+ +
var x = function(y) {
+   return y * y;
+};
+
+ +

Expressió d'una funció amb nom

+ +

Si vols fer referència a la funció actual dins del cos de la funció, necessitaràs crear una expressió d'una funció amb nom. Aquest nom és llavors només local pel cos de la funció (àmbit). AIxò també evita utilitzar la propietat no estàndard arguments.callee.

+ +
var math = {
+  'factorial': function factorial(n) {
+    if (n <= 1)
+      return 1;
+    return n * factorial(n - 1);
+  }
+};
+
+ +

Especificacions

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
EspecificacióEstatComentaris
{{SpecName('ESDraft', '#sec-function-definitions', 'Function definitions')}}{{Spec2('ESDraft')}} 
{{SpecName('ES6', '#sec-function-definitions', 'Function definitions')}}{{Spec2('ES6')}} 
{{SpecName('ES5.1', '#sec-13', 'Function definition')}}{{Spec2('ES5.1')}} 
{{SpecName('ES3', '#sec-13', 'Function definition')}}{{Spec2('ES3')}}Definició inicial. Implementat en JavaScript 1.5.
+ +

Compatibilitat amb navegadors

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
CaracterísticaChromeFirefox (Gecko)Internet ExplorerOperaSafari
Suport bàsic{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
CaracterísticaAndroidChrome per AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Suport bàsic{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

Vegeu també

+ + -- cgit v1.2.3-54-g00ecf