Skip to content

Commit 547854a

Browse files
committed
up
1 parent 4bca225 commit 547854a

File tree

1,655 files changed

+813
-89197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,655 files changed

+813
-89197
lines changed

1-js/1-getting-started/1-intro/article.md

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,95 @@
11
# An introduction to JavaScript
22

3-
Let's see what's so special about JavaScript, what we can achieve with it and what other technologies coexist with it.
3+
Let's see what's so special about JavaScript, what we can achieve with it and which other technologies play well with it.
44

55
## What is JavaScript?
66

77
*JavaScript* was initially created to *"make webpages alive"*.
88

9-
The programs in this language are called *scripts*. They are put directly into HTML and execute automatically as it loads.
9+
The programs in this language are called *scripts*. They can be written right in the HTML and execute automatically as the page loads.
1010

11-
Scripts are provided and executed a plain text. They don't need a special preparation or compilation to run.
11+
Scripts are provided and executed a plain text. They don't need a special preparation or a compilation to run.
1212

1313
In this aspect, JavaScript is very different from another language called [Java](http://en.wikipedia.org/wiki/Java).
1414

1515
[smart header="Why <u>Java</u>Script?"]
1616
When JavaScript was created, it initially had another name: "LiveScript". But Java language was very popular at that time, so it was decided that positioning a new language as a "younger brother" of Java would help.
1717

18-
But as it evolved, JavaScript became a fully independent language, with its own specification called [ECMAScript](http://en.wikipedia.org/wiki/ECMAScript), and now it has no relation to Java altogether.
18+
But as it evolved, JavaScript became a fully independent language, with its own specification called [ECMAScript](http://en.wikipedia.org/wiki/ECMAScript), and now it has no relation to Java at all.
1919

20-
It has quite a few special features that make mastering a bit hard at first, but we'll nicely deal with them later.
2120
[/smart]
2221

23-
Since the time of its creation, JavaScript evolved.
22+
At present, JavaScript can execute not only in the browser, but also on the server, or actually on any device where a special program called [an interpreter]("http://en.wikipedia.org/wiki/Interpreter_(computing)") is installed. The execution process is called "an interpretation".
2423

25-
As of now, JavaScript can execute not only in the browser, but also on the server, or actually on any device where a special program called [an interpreter]("http://en.wikipedia.org/wiki/Interpreter_(computing)") is installed. The execution process is called "an interpretation".
26-
27-
The browser has an embedded JavaScript interpreter, of course. Sometimes it's also called a *JavaScript engine* or a "JavaScript virtual machine".
24+
The browser has an embedded JavaScript interpreter, sometimes it's also called a "JavaScript engine" or a "JavaScript virtual machine".
2825

2926
Different engines have different "codenames", for example:
3027
<ul>
31-
<li>Chrome and Opera browsers and Node.JS server use [V8 engine]("https://en.wikipedia.org/wiki/V8_(JavaScript_engine)") (hence the same support for modern features).</li>
32-
<li>Firefox browser uses [Gecko]("https://en.wikipedia.org/wiki/Gecko_(software)").</li>
28+
<li>[V8 engine]("https://en.wikipedia.org/wiki/V8_(JavaScript_engine)") -- in Chrome and Opera.</li>
29+
<li>[Gecko]("https://en.wikipedia.org/wiki/Gecko_(software)") -- in Firefox.</li>
3330
<li>...There are other codenames like "Trident", "Chakra" for different versions of IE, "Nitro" and "SquirrelFish" for Safari etc.</li>
3431
</ul>
3532

36-
The codenames are usually used when searching for detailed information in the internet. Also, we'll use them further to be more exact. Instead of the words "Chrome supports feature..." we'd rather say "V8 supports feature...", not just because it's more precise, but because that also implies Opera and Node.JS.
33+
The codenames are good to know. They are used when searching for detailed information in the internet. Also, we'll sometimes reference them further in the tutorial. Instead of the words "Chrome supports feature..." we'd rather say "V8 supports feature...", not just because it's more precise, but because that also implies Opera and Node.JS.
3734

3835
[smart header="Compilation and interpretation"]
39-
There are in fact two general approaches to execute programs: "compilers" and "interpreters".
36+
There are two general approaches to execute programs: "compilation" and "interpretation".
4037

4138
<ul>
42-
<li>*Compilers* convert the program text (source code) to binary code (usually) without executing it. This is done by the developer and then the binary code is distributed to the system which actually runs it.</li>
43-
<li>*Interpreters*, and in particular the one embedded in the browser -- get the source code and execute it "as is". The source code (script) is distributed to the system as a plain text.</li>
39+
<li>*Compilers* convert the program text (source code) to binary code (or kind-of) without executing it. When a developer wants to publish the program, he runs a compiler with the source code and then distributes the binary files that it produces.</li>
40+
<li>*Interpreters*, and in particular the one embedded in the browser -- get the source code and execute it "as is".</li>
4441
</ul>
4542

46-
Modern interpreters actually combine these approaches into one: the script is distributed as a plain text, but prior to execution is converted to the machine language. That's why JavaScript executes very fast.
43+
As we can see, an interpretation is simpler. No intermediate steps involved. But a compilation is more powerful, because the binary code is more "machine-friendly" and runs faster at the end user.
44+
45+
Modern javascript engines actually combine these approaches into one:
46+
<ol>
47+
<li>The script is written and distributed as a plain text (can be compressed/optimized by so-called "javascript minifiers").</li>
48+
<li>The engine (in-browser for the web) reads the script and converts it to the machine language. And then it runs it. That's why JavaScript executes very fast.
49+
50+
Even more than that, the binary code may be adjusted later, through the process of its execution. The engine learns more about the actual data that it works with and then can optimize it better.</li>
51+
</ol>
52+
53+
So the term "interpretation" is used mostly for historical reasons. We do know what there's actually a two-stage (at least) process behind it.
4754
[/smart]
4855

4956

5057
## What in-browser JavaScript can do?
5158

5259
The modern JavaScript is a "safe" programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it.
5360

54-
Other capabilities depend on the environment which runs JavaScript. For instance, Node.JS has functionality that allows JavaScript to read/write arbitrary files, perform network requests etc etc.
61+
Other capabilities depend on the environment which runs JavaScript. For instance, Node.JS has functionality that allows JavaScript to read/write arbitrary files, perform network requests etc.
5562

5663
In the browser JavaScript can do everything related to webpage manipulation, interaction with the user and the webserver.
5764

58-
In more details, in-browser JavaScript is able to:
65+
For instance, in-browser JavaScript is able to:
5966

6067
<ul>
61-
<li>Create new HTML tags, remove the existing ones, change styles, hide/show elements...</li>
62-
<li>React on user actions, run on mouse clicks, pointer movements, key presses...</li>
63-
<li>Send requests over the network to remote servers, download and upload data without reloading the page (a so-called "AJAX" technology)...</li>
64-
<li>Get and set cookies, ask for data, show messages...</li>
65-
<li>...and can actually do almost anything with the page and it's content!</li>
68+
<li>Add new HTML to the page, change the existing content, modify styles.</li>
69+
<li>React on user actions, run on mouse clicks, pointer movements, key presses.</li>
70+
<li>Send requests over the network to remote servers, download and upload data without reloading the page (a so-called "AJAX" technology).</li>
71+
<li>Get and set cookies, prompt user for the data, show messages.</li>
72+
<li>Store data in-browser ("localStorage").</li>
6673
</ul>
6774

6875
## What in-browser JavaScript can NOT do?
6976

70-
JavaScript abilities in the browser are limited. That is for user safety, mainly not to let an evil webpage access private information or harm the user's data.
77+
JavaScript abilities in the browser are limited for the sake of the user's safety. The aim is to prevent an evil webpage from accessing private information or harming the user's data.
78+
79+
The examples of such restrictions are:
7180

7281
<ul>
7382
<li>JavaScript on the webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS system functions.
7483

75-
Modern browsers allow it to work with files, but limit the access to a specially created directory called "a sandbox".
84+
Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like "dropping" a file into a browser window or selecting it via an `<input>` tag.
7685

77-
There are ways to interact with camera/microphone and other devices, but they require an explicit user's permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to NSA.
86+
There are ways to interact with camera/microphone and other devices, but they require an explicit user's permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the NSA.
7887
</li>
79-
<li>JavaScript may not freely access other pages opened in the same browser. The exception is when the pages come from the same site.
88+
<li>Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. Such action is allowed. But even in this case, JavaScript from one page may not access the other if they compe from different sites (from a different domain, protocol or port).
8089

81-
There are ways to workaround this, of course. But if two pages come from different sites (different domain, protocol or port), they require a special code on *both of them* allowing to interact.
90+
That is called a "Same Origin Policy". To workaround that, *both pages* must contain a special JavaScript code that handles data exchange.
8291

83-
The limitation is again for user safety. A page from `http://evilsite.com` which a user has opened occasionaly will be unable to access other browser tabs and steal information from there.
92+
The limitation is again for a user's safety. A page from `http://anysite.com` which a user has opened occasionaly must not be able to open or access another browser tab with the URL `http://gmail.com` and steal information from there.
8493
</li>
8594
<li>JavaScript can easily communicate over the net to the server where the current page came from. But it's ability to receive data from other sites/domains is crippled. Though possible, it requires the explicit agreement (expressed in HTTP headers) from the remote side. Once again, that's safety limitations.
8695
</li>
@@ -89,7 +98,7 @@ The limitation is again for user safety. A page from `http://evilsite.com` which
8998
<img src="limitations.png">
9099

91100

92-
Such limits do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow installing plugin/extensions which get extended permissions.
101+
Such limits do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow installing plugin/extensions which may get extended permissions.
93102

94103

95104
## Why JavaScript is unique?
@@ -106,7 +115,7 @@ Combined, these 3 things only exist in JavaScript and no other browser technolog
106115

107116
That's what makes JavaScript unique. That's why it is the most widespread way of creating browser interfaces.
108117

109-
Of course, there are certain trends including new languages and browser abilities. While planning to learn a new technology, it's beneficial to check it's perspectives, so we go ahead with that.
118+
While planning to learn a new technology, it's beneficial to check it's perspectives. So let's move on to the modern trends that include new languages and browser abilities.
110119

111120
## HTML 5
112121

@@ -115,7 +124,7 @@ Of course, there are certain trends including new languages and browser abilitie
115124
Few examples:
116125

117126
<ul>
118-
<li>Read/write files on disk (in a "sandbox", not just any file).</li>
127+
<li>Write files on disk (in a "sandbox", not to any folder).</li>
119128
<li>A database embedded in the browser, to keep data on a user's computer and effeciently operate on it.</li>
120129
<li>Multitasking with the usage of many CPU cores in one time.</li>
121130
<li>Audio/video playback.</li>
@@ -128,9 +137,9 @@ Many new abilities are still in progress, but browsers gradually improve the sup
128137
The trend: browser can do more and more, it is becoming more like an all-purpose desktop application.
129138
[/summary]
130139

131-
Still, there is a small gotcha with those "extra-fresh" modern browser abilities. Sometimes browsers try to implement them on very early stages when they are nor fully defined neither agreed upon, but still are so interesting that the developers just can't wait.
140+
Still, there is a small gotcha with those "extra-fresh" modern browser abilities. Sometimes browsers try to implement them on very early stages when they are nor fully defined neither agreed upon, but are so interesting that the developers just can't wait.
132141

133-
...As the time goes, the specification matures and changes, and browsers must adapt it. That may lead to errors in the older code which was too eager to use the early browser implementation. So one should think twice before relying on things that are in draft yet.
142+
...As the time goes, the specification matures and changes, and browsers must adapt it. That may lead to errors in the older code which was too eager to use the early version. So one should think twice before relying on things that are in draft yet.
134143

135144
But what's great -- eventually all browsers tend to follow the standard. There are much less differences between them now than only a couple years ago.
136145

@@ -152,7 +161,7 @@ The trend: JavaScript is becoming faster, gets new syntax and language features.
152161

153162
The syntax of JavaScript does not suit everyone's needs: some people think that it's too flexible, the others consider it too limited, the third ones want to add new features absent in the standard...
154163

155-
That's normal, because projects and requirements are different for everyone. There's no a single standard for a carpenter's hammer, why should it exist for the language?
164+
That's normal, because projects and requirements are different for everyone.
156165

157166
So recently a plethora of new languages appeared, which are *transpiled* (converted) to JavaScript before they run.
158167

@@ -163,7 +172,7 @@ Examples of such languages:
163172
<ul>
164173
<li>[CoffeeScript](http://coffeescript.org/) is a "syntax sugar" for JavaScript, it introduces shorter syntax, allowing to write more precise and clear code. Usually Ruby guys like it.</li>
165174
<li>[TypeScript](http://www.typescriptlang.org/) is concentrated on adding "strict data typing", to simplify development and support of complex systems. Developed by Microsoft.</li>
166-
<li>[Dart](https://www.dartlang.org/) was offered by Google as a replacement for JavaScript, but other leading internet companies declared that they are not interested. Maybe later, we'll see. Right now it can be transpiled to JavaScript, but used less often compared to two previous alternatives.</li>
175+
<li>[Dart](https://www.dartlang.org/) is a standalone language that has it's own engine that runs in non-browser environments (like mobile apps). It was initially offered by Google as a replacement for JavaScript, but as of browsers require it to be transpiled to JavaScript just like the ones above.</li>
167176
</ul>
168177

169178
## Summary

0 commit comments

Comments
 (0)