From 4b1a9203c547c019fc5398082ae19a3f3d4c3efe Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:41:15 -0500 Subject: initial commit --- .../referencia/sentencies/function/index.html | 171 +++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 files/ca/web/javascript/referencia/sentencies/function/index.html (limited to 'files/ca/web/javascript/referencia/sentencies/function/index.html') diff --git a/files/ca/web/javascript/referencia/sentencies/function/index.html b/files/ca/web/javascript/referencia/sentencies/function/index.html new file mode 100644 index 0000000000..37deff748f --- /dev/null +++ b/files/ca/web/javascript/referencia/sentencies/function/index.html @@ -0,0 +1,171 @@ +--- +title: function +slug: Web/JavaScript/Referencia/Sentencies/function +translation_of: Web/JavaScript/Reference/Statements/function +--- +
+
{{jsSidebar("Statements")}}
+
+ +

Resum

+ +

La declaració d'una funció defineix una funció amb uns paràmetres especificats.

+ +
+

També podeu definir funcions fent servir el constructor {{jsxref("Function")}} i un {{jsxref("Operators/function", "function expression")}}.

+
+ +

Sintaxi

+ +
function nom([paràm,[, paràm,[..., paràm]]]) {
+   [sentències]
+}
+
+ +
+
nom
+
El nom de la funció.
+
+ +
+
paràm
+
El nom d'un argument que se li passarà a la funció. Una funció pot arribar a tenir fins a 255 arguments.
+
+ +
+
sentències
+
Les sentències que comprenen el cos de la funció.
+
+ +

Descripció

+ +

Una funció creada amb una declaració d'una funció és un objecte Function i té totes les propietats, mètodes i comportament dels objectes Function. Vegeu {{jsxref("Function")}} per informació detallada sobre funcions.

+ +

Una funció també es pot crear fent servir una expressió (vegeu {{jsxref("Operators/function", "function expression")}}).

+ +

Per defecte, les funcions retornen undefined. Per tal de retornar qualsevol altre valor, la funció ha de tenir una sentència {{jsxref("Statements/return", "return")}} que especifiqui el valor que retorna.

+ +

Funcions creades de forma condicional

+ +

Les funcions poden ser declarades de forma condicional, és a dir, una sentència d'una funció pot estar aniuada dins d'una sentència if. La majoria de navegadors que no siguin Mozilla tractaran aquestes declaracions condicionals com a declaracions incondicionals i crearàn la funció tant si la condició és vertadera o falsa, vegeu aquest article per una visió general. Per tant, no s'haurien de fer servir, per creacions condicionals feu servir expressions de funcions.

+ +

Declarar les funcions abans de definir-les (hosting)

+ +

Function declarations in JavaScript are hoisting the function definition. En Javascript es pot ser cridar una funció abans de declarar-la. En anglès existeix un ver:

+ +
hoisted(); // logs "foo"
+
+function hoisted() {
+  console.log("foo");
+}
+
+ +

Vegeu que {{jsxref("Operators/function", "function expressions")}} no estan hoisted:

+ +
notHoisted(); // TypeError: notHoisted is not a function
+
+var notHoisted = function() {
+   console.log("bar");
+};
+
+ +

Exemples

+ +

Exemple: Fer servir function

+ +

El codi següent declara una funció que retorna la quantitat total de vendes, quan se li dóna el nombre d'unitat venudes d' a, b, i c.

+ +
function calc_sales(units_a, units_b, units_c) {
+   return units_a*79 + units_b * 129 + units_c * 699;
+}
+
+ +

Especificacions

+ + + + + + + + + + + + + + + + + + + + + + + + +
EspecificacióEstatComentaris
1a edició de ECMAScript.EstàndardDefinició iniciañ. Implementat en JavaScript 1.0
{{SpecName('ES5.1', '#sec-13', 'Function definition')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-function-definitions', 'Function definitions')}}{{Spec2('ES6')}} 
+ +

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