ما هي الـجافا سكربت JavaScript؟

قُدِّمت الجافا سكربت في عام ١٩٩٥ كطريقة لإضافة برامج لصفحات الشَّبكة web في متصفِّح Netscape Navigator browser. ومنذ ذلك الحين تَمَّ تبنِّي اللُّغة من قبل كل متصفِّحات الشَّبكة الرُّسوميَّة. لقد جعلت تطبيقات الشَّبكة الحديثة ممكنة—تطبيقاتٌ يمكنك التَّفاعل معها مباشرةً، دون الحاجة للقيام بإعادة تحميل الصَّفحة لكلِّ فعل. لكنَّها أيضًا مُستخدمة في العديد من المواقع التقليديَّة لتوفير أنماط مختلفة من التفاعليَّة والحذاقة.

من المهم ذِكر أنَّه ليسَ للجافا سكربت أيُّ علاقةٍ بلغةِ البرمجة المُسَمَّاة جافا Java. الإسمُ المُشابِهُ أُستُلهِمَ لإعتباراتٍ تسويقيَّة، أكثر مِمَّا هو قرارٌ صائب. عندما قُدِّمت الجافا سكربت، كانت لغة جافا مُسَوَّقة بقوَّة وحاصِلة على الشَّعبيَّة. إعتقدَ شخصٌ ما أنَّ رُكوب موجة النجاح هذه شيءٌ جيِّد. الآن نحن عالِقون مع الإسم.

بعد تبنِّيها خارج Netscape، كُتبت وثيقة مِعياريَّة لوصف الطَّريقة الَّتي يجب أن تعمل بها JavaScript لضمان دعم أجزاء البرمجيَّة software المختلفة الَّتي تزعُم دعمها للجافا سكربت والحديث مازال قائمًا عن نفس اللُّغة. بعد قيام منظَّمة Ecma العالميَّة بالتقييس standardization، دُعيت ECMAScript standard. عمليًّا، يمكن إستخدام المصطلحين ECMAScript وَ JavaScript بشكل مُتبادَل—هُما إسمان لِذاتِ اللُّغة. هنالك أشخاص سيقولن أشياءً فظيعة عن لغة الجافا سكربت. العديد من هذه الأشياء صحيحة. عندما تَطَلَّبَ منِّي كتابة شيء ما في الجافا سكربت للمرَّة الأُولى، بسرعة أصبحتُ أذدريها. كانت تقبل كل شيء أكتبه تقريبًا لكن تفسِّره interpret بطريقة مختلفة عُمَّا أعنيه. لهذا علاقة وثيقة بأنني لم أكن أعي ما أفعله، بالطبع، ولكن هناك قضيَّة حقيقيَّة هُنا: الجافا سكربت متحرِّرة للغاية بما تسمح به. الفكرة خلف هذا التصميم جعل البرمجة بلغة الجافا سكربت أسهل للمُبتدئين. في الواقع، غالبًا تجعل إيجاد المشاكل في برنامجك أصعب لأنَّ النظام لن يُشير لك إليها.

برغم ذلك، لهذه المرونة فوائدها أيضًا. تترك مجالًا للكثير من التقنيَّات المستحيلة بلغاتٍ جامدة أُخرى، وكما سترى (كما في الفصل ١٠ على سبيل المثال) يمكن إستخدامها للتغلُّب على بعض عيوب الجافا سكربت. بعد التعلُّم المُلائم للُّغة والعمل بها لفترة، تعلَّمتُ أن أُحِبَّ الجافا سكربت. كان هناك العديد من إصدارات الجافا سكربت. كان أكثرُها دعمًا في زمن عُلوِّها نحو الهيمنة الإصدار ECMAScript 3، تقريبًا بين عامي ٢٠٠٠ وَ ٢٠١٠. خلال هذا الوقت، كان العمل جاريًا على إصدارٍ واعِدٍ 4، الَّذي خَطَّ عددًا من التحسينات الجذريَّة والإمتدادات للُّغة. بانَت محاولة تغيير لغة حياتيَّة، واسعة الإستخدام بطريقة جذريَّة أنَّها صعبة سياسيًّا، وكان العمل على الإصدار 4 مهجورًا في ٢٠٠٨، مؤدِّيًا للإصدار 5 الأقل طموحًا الذي صدر في ٢٠٠٩. نحن الآن بصدد نقطة دعم كل المتصفِّحات الرَّئيسيَّة للإصدار 5، والذي هو الإصدار الذي سيركِّز عليه هذا الكتاب. شارف الإنتهاء من تجهيز الإصدار 6، وبدأت بعض المتصفِّحات بدعم ميِّزاتٍ جديدةٍ من هذا الإصدار.

ليست متصفِّحات الشَّبكة المِنَصَّة الوحيدة الَّتي تستخدم الجافا سكربت. بعض قواعد البيانات، مثل MongoDB وَ CouchDB، تستخدم الجافا سكربت كلغتها الإستفساريَّة والسكربتيَّة. العديد من مِنَصَّات برمجة سطح المكتب والخوادم، وعلى نحوٍ خاص المشروع Node.js (وهو موضوع الفصل 20) توفِّر بيئة قويَّة للبرمجة بالجافا سكربت خارج المُتَصَفِّحات.

What is JavaScript?

JavaScript was introduced in 1995 as a way to add programs to web pages

in the Netscape Navigator browser. The language has since been adopted

by all other major graphical web browsers. It has made modern web applications possible—applications with which you can interact directly,

without doing a page reload for every action. But it is also used in

more traditional websites to provide various forms of interactivity and

cleverness.

It is important to note that JavaScript has almost nothing to do

with the programming language named Java. The similar name was in-

spired by marketing considerations, rather than good judgment. When

JavaScript was being introduced, the Java language was being heavily

marketed and was gaining popularity. Someone thought it was a good

idea to try to ride along on this success. Now we are stuck with the

name.

After its adoption outside of Netscape, a standard document was writ-

ten to describe the way the JavaScript language should work to make

sure the various pieces of software that claimed to support JavaScript

were actually talking about the same language. This is called the EC-

MAScript standard, after the Ecma International organization that did

the standardization. In practice, the terms ECMAScript and JavaScript

can be used interchangeably—they are two names for the same language.

There are those who will say terrible things about the JavaScript lan-

guage. Many of these things are true. When I was required to write

something in JavaScript for the first time, I quickly came to despise it.

It would accept almost anything I typed but interpret it in a way that

was completely different from what I meant. This had a lot to do with

the fact that I did not have a clue what I was doing, of course, but there

is a real issue here: JavaScript is ridiculously liberal in what it allows.

The idea behind this design was that it would make programming in

JavaScript easier for beginners. In actuality, it mostly makes finding

problems in your programs harder because the system will not point

them out to you.

This flexibility also has its advantages, though. It leaves space for a

lot of techniques that are impossible in more rigid languages, and as you

will see (for example in Chapter 10) it can be used to overcome some

of JavaScript’s shortcomings. After learning the language properly and

working with it for a while, I have learned to actually like JavaScript.

There have been several versions of JavaScript. ECMAScript version

3 was the widely supported version in the time of JavaScript’s ascent

to dominance, roughly between 2000 and 2010. During this time, work was underway on an ambitious version 4, which planned a number of

radical improvements and extensions to the language. Changing a living,

widely used language in such a radical way turned out to be politically

difficult, and work on the version 4 was abandoned in 2008, leading to

the much less ambitious version 5 coming out in 2009. We’re now at the

point where all major browsers support version 5, which is the language

version that this book will be focusing on. A version 6 is in the process of

being finalized, and some browsers are starting to support new features

from this version.

Web browsers are not the only platforms on which JavaScript is used.

Some databases, such as MongoDB and CouchDB, use JavaScript as

their scripting and query language. Several platforms for desktop and

server programming, most notably the Node.js project (the subject of

Chapter 20) are providing a powerful environment for programming

JavaScript outside of the browser.