8. الوظائف

8.1. البرنامج النصي [func-01]
يفحص البرنامج النصي كيفية تمرير المعلمات إلى الدالة:
- التمرير بالقيمة للأرقام والسلاسل والقيم المنطقية؛
- التمرير بالمرجع للمصفوفات، والأشياء الحرفية، والوظائف؛
'use strict';
// how to pass function parameters
// -----------------------nombre - passing by value
function doSomethingWithNumber(param) {
param++;
console.log("[param inside function]=", param, "[type]=", typeof (param), "[passage par référence]=", param === count);
}
// call code
let count = 10;
doSomethingWithNumber(count);
console.log("[count outside function]=", count);
// --------------------- chain - passing by value
function doSomethingWithString(param) {
param += " xyz"
console.log("[param inside function]=", param, "[type]=", typeof (param), "[passage par référence]=", param === text);
}
// call code
let text = "abcd";
doSomethingWithString(text);
console.log("[text outside function]=", text);
// --------------------- Boolean - passing by value
function doSomethingWithBoolean(param) {
param = !param;
console.log("[param inside function]=", param, "[type]=", typeof (param), "[passage par référence]=", param === bool);
}
// call code
let bool = true;
doSomethingWithBoolean(bool);
console.log("bool [outside function]=", bool);
// --------------------- table - passage by reference
function doSomethingWithArray(param) {
param.push(1000);
console.log("[param inside function]=", param, "[type]=", typeof (param), "[passage par référence]=", param === tab);
}
// call code
const tab = [10, 20, 30];
doSomethingWithArray(tab);
console.log("[tab outside function]=", tab);
// --------------------- object - passing by reference
function doSomethingWithObject(param) {
param.unePropriétéNouvelle = "xyz";
console.log("[param inside function]=", param, "[type]=", typeof (param), "[passage par référence]=", param === obj);
}
// call code
const obj = [10, 20, 30];
doSomethingWithObject(obj);
console.log("[obj outside function]=", obj);
// --------------------- function - pass by reference
function doSomethingWithFunction(param) {
// a rather bizarre thing that works
param.unePropriétéNouvelle = "xyz";
console.log("[param inside function]=", param, "[type]=", typeof (param), "[passage par référence]=", param === f);
}
// call code
const f = x => x + 4;
doSomethingWithFunction(f);
console.log("[f outside function]=", f, f.unePropriétéNouvelle, typeof (f));
التنفيذ
[Running] C:\myprograms\laragon-lite\bin\nodejs\node-v10\node.exe -r esm "c:\Data\st-2019\dev\es6\javascript\fonctions\func-01.js"
[param inside function]= 11 [type]= number [passage par référence]= false
[count outside function]= 10
[param inside function]= abcd xyz [type]= string [passage par référence]= false
[text outside function]= abcd
[param inside function]= false [type]= boolean [passage par référence]= false
bool [outside function]= true
[param inside function]= [ 10, 20, 30, 1000 ] [type]= object [passage par référence]= true
[tab outside function]= [ 10, 20, 30, 1000 ]
[param inside function]= [ 10, 20, 30, 'unePropriétéNouvelle': 'xyz' ] [type]= object [passage par référence]= true
[obj outside function]= [ 10, 20, 30, 'unePropriétéNouvelle': 'xyz' ]
[param inside function]= x => x + 4 [type]= function [passage par référence]= true
[f outside function]= x => x + 4 xyz function
8.2. نص برمجي [func-02]
يوضح البرنامج النصي التالي أن نوع [function] هو نوع بيانات مثل أي نوع آخر وأن المتغير يمكن أن يكون من هذا النوع. كما يوضح طريقتين لتعريف الدالة:
- إحداهما باستخدام الكلمة الرئيسية [function]؛
- والأخرى باستخدام رمز "السهم" =>؛
'use strict';
// you can assign a function to a variable
const variable1 = function (a, b) {
return a + b;
};
console.log("typeof(variable1)=", typeof (variable1));
// the variable can then be used as a function
console.log("variable1(10,12)=", variable1(10, 12));
// the function can be defined using the notation =>
const variable2 = (a, b, c) => {
return a - b + c;
};
console.log("variable2(10,12,14)=", variable2(10, 12, 14));
// braces can be omitted if there is only one expression in the function code
// this expression is then the return value of the
const variable3 = (a, b, c) => a + b + c;
console.log("variable3(10,12,14)=", variable3(10, 12, 14));
التنفيذ
[Running] C:\myprograms\laragon-lite\bin\nodejs\node-v10\node.exe -r esm "c:\Data\st-2019\dev\es6\javascript\fonctions\func-02.js"
typeof(variable1)= function
variable1(10,12)= 22
variable2(10,12,14)= 12
variable3(10,12,14)= 36
8.3. نص برمجي [func-03]
يوضح هذا البرنامج النصي القدرة على تمرير دالة كمعلمة إلى دالة أخرى. تُستخدم هذه التقنية على نطاق واسع في أطر عمل JavaScript.
'use strict';
// function parameters can be of type [function]
// function f1
function f1(param1, param2) {
return param1 + param2 + 10;
}
// function f2
function f2(param1, param2) {
return param1 + param2 + 20;
}
// function g with a function f as parameter
function g(param1, param2, f) {
return f(param1, param2) + 100;
}
// uses of g
console.log(g(0, 10, f1));
console.log(g(0, 10, f2));
// the function-type effective parameter can be passed directly - form 1
console.log(g(0, 10, (param1, param2) => {
return param1 + param2 + 30;
}));
// the function-type effective parameter can be passed directly - form 2
console.log(g(0, 10, function (param1, param2) {
return param1 + param2 + 40;
}));
التنفيذ
[Running] C:\myprograms\laragon-lite\bin\nodejs\node-v10\node.exe -r esm "c:\Data\st-2019\dev\es6\javascript\fonctions\func-03.js"
120
130
140
150
8.4. نص برمجي [func-04]
يوضح البرنامج النصي التالي أن دالة JavaScript يمكن أن تتصرف كفئة:
'use strict';
// a function can be used as an object
// an empty shell
function f() {
}
// to which external properties are attributed
f.prop1 = "val1";
f.show = function () {
console.log(this.prop1);
}
// use of f
f.show();
// a function g operating as a class
function g() {
this.prop2 = "val2";
this.show = function () {
console.log(this.prop2);
}
}
// instantiate the function with [new]
new g().show();
تعليقات
- الأسطر 5–7: لا يحدد نص الدالة f أي خصائص؛
- الأسطر 9–12: يتم تعيين الخصائص للدالة f من الخارج؛
- السطر 14: استخدام الدالة (الكائن) f. لاحظ أننا لا نكتب [f()] بل نكتفي بكتابة [f]. هذه هي صيغة الكائن؛
- الأسطر 17–22: نُعرّف الدالة [g] كما لو كانت فئة ذات خصائص وطرق؛
- السطر 24: يتم إنشاء مثيل للدالة [g] بواسطة [new g()]؛
نتائج التنفيذ
[Running] C:\myprograms\laragon-lite\bin\nodejs\node-v10\node.exe -r esm "c:\Data\st-2019\dev\es6\javascript\classes\class-00.js"
val1
val2
أدخلت ES6 مفهوم الفئات، مما يسمح لنا الآن بتجنب استخدام الدوال لإنشاء الفئات.
8.5. نص برمجي [func-05]
يوضح البرنامج النصي [func-05] استخدام عامل يُسمى [عامل الباقي]:
'use strict';
// rest operator
function f(arg1, ...otherArgs) {
// 1st argument
console.log("arg1=", arg1);
// other arguments
let i = 0;
otherArgs.forEach(element => {
console.log("otherArguments[", i, "]=", element);
i++;
});
}
// call
f(1, "deux", "trois", { x: 2, y: 3 })
- السطر 3: تعني صيغة [...otherArgs] أنه عند استدعاء الدالة بالصيغة f(param1, param2, param3)، سيكون لدينا السطر 3:
- arg1=param1
- otherArgs = [param2, param3]. وبالتالي، فإن [otherArgs] عبارة عن مصفوفة تجمع جميع المعلمات الفعلية التي تم تمريرها بعد [param1]؛
نتائج التطبيق هي كما يلي: