JavaScript: Send function as a parameter to another function (callbacks) جافا سكريبت : إرسال الدالة كمعلمة إلى وظيفة أخرى (رد)
Posted on 29. نشر في 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax يوليو ، 2009 من قبل دراكوش في الترميز ، جافا سكريبت واياكس
I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: أنا متأكد من أنك رأيت الكثير من الوظائف البرمجية ، حيث يتم إرسال والمعلمات ، وعادة ما يعمل رد الدالة على سبيل المثال :
setTimeout(function () { alert('test'); },1000); setTimeout (وظيفة)) (تنبيه ( 'اختبار') ؛) ، 1000) ؛
But how does the function setTimeout execute the passed function as a parameter? ولكن كيف يمكن للsetTimeout الدالة تنفيذ الدالة تمريره كمعلمة؟ The answer is simple, but I need to provide you an example to understand it الجواب بسيط ، ولكني في حاجة لتوفر لك مثالا لفهمه ![]()
Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. دعونا إنشاء وظيفة بسيطة ، وقبول معلمتين : أولا قيمة منطقية ، والثانية -- وظيفة. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. لدينا وظيفة سوف يحلل المعلمة ومنطقية في حال كانت القيمة الحقيقية ، وظيفة تم تمريره كمعلمة سيتم تنفيذها.
function simpleFunc(bool,func) { وظيفة simpleFunc (bool ، ظائفه) ( if(bool) func(); إذا كان (bool) ظائفه () ؛ } )
As you notice, we add parentheses after the name of the second parameter, because we'll treat it as a function. كما تلاحظون ، فإننا إضافة قوسين بعد اسم المعلمة الثانية ، لأننا سوف نتعامل مع المسألة باعتبارها وظيفة. And that's te whole secret. وهذا هو الشركة المصرية للاتصالات سرية كاملة.
Now that is how we will use the simpleFunc function: الآن هذه هي الكيفية التي سوف نستخدم الدالة simpleFunc :
simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (كاذبة ، وظيفة () (تنبيه ( 'Yupee!') ؛) ؛ / / هذا لن تنبيه أي شيء ، لأن المعلمة منطقية هو زائف //example two / / سبيل المثال اثنين simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (صحيح ، والدالة () (تنبيه ( 'Yupee!') ؛)) / / هذا وسوف تنبيه Yupee!![]()
Wish you luck! أتمنى لكم حظا سعيدا!
Related posts: الوظائف ذات الصلة :
- Javascript: How to validate email address with JavaScript? جافا سكريبت : كيفية التحقق من صحة عنوان البريد الإلكتروني مع جافا سكريبت؟
- JavaScript: How to get the index (position within a group) of an object with jQuery? جافا سكريبت : كيفية الحصول على مؤشر (الموقف داخل مجموعة) للجسم مع jQuery؟
- JavaScript: What if jQuery animation doesn't fire/start? جافا سكريبت : ماذا لو الرسوم المتحركة jQuery لا النار / تبدأ؟
- Coding: How to get code suggestions and function completion in Netbeans? الترميز : كيفية الحصول على وظيفة الاقتراحات رمز الاكتمال في Netbeans؟
- Coding:How to fetch user profile data with SSI.php from a SMF forum database الترميز : كيفية إحضار البيانات الجانبي للمستخدم مع SSI.php بإمكانك زيارة المجلة الثقافية من قاعدة بيانات منتدى












































