شرح صفوف ومحتوى الفقرة
- حياة المتغير يبدأ عندما يتم الأعلان عنه declare مباشرة.
- المتغيرات variables من النوع المحلي local scope أو function scope يتم حذفة بعد انتهاء تنفيذ الدالة function.
- المتغيرات variables من النوع global scope يتم حذفها عندما يتم اغلاق صفحة الويب من المتصفح.
- المتغيرات variables من النوع global scope يتم اعادة الأعلان عنها عندما يتم فتح صفحة الويب مرة اخري علي المتصفح.
- المتغيرات variables من النوع global scope يتم استخدامها من داخل ملف جافا سكريبت اخر اذا تم أستدعاء الملف الذي يحمل المتغير أعلي الملف الذي يستدعي منه شاهد المثالي التالي.
use variables in another js file
يمكن استدعاء أي متغير من النوع النطاق العام global variable تم الأعلان عنه في ملف من أي ملف جافا سكربت اخر بشرط الاستدعاء اسفل الملف.
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Global Scope</h1>
<script src="https://www.closetag.com/examples/global.js"></script>
<script>
/*
x is decleared in another file
x is decleared here -> https://www.closetag.com/examples/global.js
use x here
use x below file.js contain x
*/
document.write("This variable is declared in external js file we will use it in this file : " + x);
</script>
</body>
</html>
use variables above another js file
سوف يحدث خطأ اثناء الاستدعاء في حالة استدعاء المتغير أعلي ملف جافا سكريبت الزي تم الأعلان عن المتغير بداخلة.
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Global Scope</h1>
<script>
/* x is undefind if use above js file */
document.write("This variable is declared in external js file we will use it in this file : ");
document.write(x);
</script>
<script src="https://www.closetag.com/examples/global.js"></script>
</body>
</html>