fix: converted single to triple backticks10 (#36237)
This commit is contained in:
@@ -6,14 +6,15 @@ localeTitle: طول الوظيفة
|
||||
|
||||
تحمل الخاصية `length` في كائن الدالة عدد الوسيطات المتوقع بواسطة الدالة عند استدعاء.
|
||||
|
||||
`function noArgs() { }
|
||||
|
||||
function oneArg(a) { }
|
||||
|
||||
console.log(noArgs.length); // 0
|
||||
|
||||
console.log(oneArg.length); // 1
|
||||
`
|
||||
```javascript
|
||||
function noArgs() { }
|
||||
|
||||
function oneArg(a) { }
|
||||
|
||||
console.log(noArgs.length); // 0
|
||||
|
||||
console.log(oneArg.length); // 1
|
||||
```
|
||||
|
||||
### ES2015 التركيب
|
||||
|
||||
@@ -21,17 +22,18 @@ ES2015 ، أو ES6 كما يطلق عليه عادة ، قدم المعلمات
|
||||
|
||||
إذا تم استخدام عامل التشغيل المتبقي أو المعلمات الافتراضية في تعريف الدالة ، فإن خاصية `length` ستشمل فقط عدد الوسيطات قبل عامل تشغيل باقي أو معلمة افتراضية.
|
||||
|
||||
`function withRest(...args) { }
|
||||
|
||||
function withArgsAndRest(a, b, ...args) { }
|
||||
|
||||
function withDefaults(a, b = 'I am the default') { }
|
||||
|
||||
console.log(withRest.length); // 0
|
||||
|
||||
console.log(withArgsAndRest.length); // 2
|
||||
|
||||
console.log(withDefaults.length); // 1
|
||||
`
|
||||
```javascript
|
||||
function withRest(...args) { }
|
||||
|
||||
function withArgsAndRest(a, b, ...args) { }
|
||||
|
||||
function withDefaults(a, b = 'I am the default') { }
|
||||
|
||||
console.log(withRest.length); // 0
|
||||
|
||||
console.log(withArgsAndRest.length); // 2
|
||||
|
||||
console.log(withDefaults.length); // 1
|
||||
```
|
||||
|
||||
يمكن العثور على مزيد من المعلومات حول `Function.length` على [مستندات MDN الخاصة بـ Mozilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length) .
|
@@ -12,29 +12,31 @@ localeTitle: Function.prototype.bind
|
||||
|
||||
يتم تكليفك بكتابة الكود لتحديث عدد الحضور عند وصولهم إلى المؤتمر. يمكنك إنشاء صفحة ويب بسيطة تحتوي على زر ، عند النقر فوق ذلك ، بزيادة `numOfAttendees` خاصية على كائن الاستجواب. يمكنك استخدام jQuery لإضافة معالج النقر إلى الزر الخاص بك ، ولكن بعد النقر فوق الزر ، لم يتغير كائن الإيداع. قد تبدو شفرتك شيئًا كهذا.
|
||||
|
||||
`var nodevember = {
|
||||
numOfAttendees: 0,
|
||||
incrementNumOfAttendees: function() {
|
||||
this.numOfAttendees++;
|
||||
}
|
||||
// other properties
|
||||
};
|
||||
|
||||
$('.add-attendee-btn').on('click', nodevember.incrementNumOfAttendees);
|
||||
`
|
||||
```javascript
|
||||
var nodevember = {
|
||||
numOfAttendees: 0,
|
||||
incrementNumOfAttendees: function() {
|
||||
this.numOfAttendees++;
|
||||
}
|
||||
// other properties
|
||||
};
|
||||
|
||||
$('.add-attendee-btn').on('click', nodevember.incrementNumOfAttendees);
|
||||
```
|
||||
|
||||
هذه مشكلة شائعة عند العمل مع jQuery و JavaScript. عند النقر فوق زر `this` الكلمة في الطريقة التي تم تمريرها إلى ومسج `on` طريقة يرجع زر وليس الكائن المؤتمر. يمكنك ربط `this` السياق الخاص بك بالطريقة لحل المشكلة.
|
||||
|
||||
`var nodevember = {
|
||||
numOfAttendees: 0,
|
||||
incrementNumOfAttendees: function() {
|
||||
this.numOfAttendees++;
|
||||
}
|
||||
// other properties
|
||||
};
|
||||
|
||||
$('.add-attendee-btn').on('click', nodevember.incrementNumOfAttendees.bind(nodevember));
|
||||
`
|
||||
```javascript
|
||||
var nodevember = {
|
||||
numOfAttendees: 0,
|
||||
incrementNumOfAttendees: function() {
|
||||
this.numOfAttendees++;
|
||||
}
|
||||
// other properties
|
||||
};
|
||||
|
||||
$('.add-attendee-btn').on('click', nodevember.incrementNumOfAttendees.bind(nodevember));
|
||||
```
|
||||
|
||||
الآن عند النقر فوق الزر يشير `this` إلى كائن `nodevember` .
|
||||
|
||||
@@ -42,14 +44,15 @@ localeTitle: Function.prototype.bind
|
||||
|
||||
كل وسيطة تم تمريرها `bind` بعد الأول سوف تسبق أية وسائط يتم تمريرها عند استدعاء الوظيفة. هذا يسمح لك بتطبيق الحجج مسبقًا على إحدى الوظائف. في المثال أدناه ، تأخذ `combineStrings` معًا. `bind` ثم يتم استخدامها لإنشاء دالة التي توفر دائما "كول" كسلسلة الأولى.
|
||||
|
||||
`function combineStrings(str1, str2) {
|
||||
return str1 + " " + str2
|
||||
}
|
||||
|
||||
var makeCool = combineStrings.bind(null, "Cool");
|
||||
|
||||
makeCool("trick"); // "Cool trick"
|
||||
`
|
||||
```javascript
|
||||
function combineStrings(str1, str2) {
|
||||
return str1 + " " + str2
|
||||
}
|
||||
|
||||
var makeCool = combineStrings.bind(null, "Cool");
|
||||
|
||||
makeCool("trick"); // "Cool trick"
|
||||
```
|
||||
|
||||
يحتوي الدليل على [هذا المرجع](https://guide.freecodecamp.org/javascript/this-reference) على مزيد من المعلومات حول كيفية تغيير ما تشير إليه `this` الكلمة الرئيسية.
|
||||
|
||||
|
Reference in New Issue
Block a user