Earlier, we talked about programming language TypeScript, created by Microsoft as a replacement for JavaScript.
This article will tell you about yet another alternative, JS - programming language Dart, created by Google.
Dart - Dart JavaScript in the back from Google
Naturally, the "good company", the developer of the engine V8 and one of the most popular browsers Google Chrome, could not get past trends "improve» JavaScript, and September 12, 2011 nakonferentsii developers Goto was held the official presentation of the language Google Dart.
Unlike analog Dart directly positioned as a replacement for JavaScript, «suffers from fundamental flaws that can not be corrected by the evolutionary development."
The language designers set themselves the following objectives: to create a structured and at the same time flexible language for web programming; make the language similar to the existing dlyauproscheniya training; provide high performance produced programs such as browsers, as well as in other environments, ranging from smartphones to servers.
Currently Dart-program executed in two ways: using a virtual machine (in the browser Google Chrome) and intermediate translation (compilation) in JavaScript.
Most interesting is that Dart aspires to become a direct competitor to the platform Node.js - a virtual machine Dart (VM Dart) is an integral part of the language. With it you can run the program Dart-in console mode, and the means to work on the server side in Dart are available in sufficient quantities.
Ecosystem Dart
Let us first consider what constitutes the language. To do this, it makes sense to get acquainted with specialized tools created by Google's tracking technologies.
On the page of the project, we can immediately see the appeal - Downlod Dart + Editor. We use this offer and obtain, download and unzip the file, a lot of interesting things.
First of all, it is a virtual machine Dart (Dart VM) - server platform, which implements event-driven, asynchronous execution model similar to as good a familiar principle of Node.js. Next Dart Editor - a complete integrated development environment (IDE), based on Eclipse. Dart Editor complemented by a web browser - a special modification of the Chromium browser with built-Dart (Dart VM) engine.
Finally, we are now available to set Dart SDK, which includes the interpreter itself dart, utility dart-broadcast applications in the JavaScript-file (dart2js), utility to generate documentation (docgen) iprochie useful tools.
Let's run the editor, create a new project (an icon in the upper left corner of the main window IDE, should choose the type of comand-line application) and write our first dart-program.There is nothing original:
Now run it for execution by clicking the arrow on the top toolbar. Judging by the line appeared «Hello Dart» Console Editor, Dart works (about the peculiarities of the language - just below).
The same result we can achieve, while maintaining the above code in a file and run it hello.dart using an interpreter from the command line:
Now you can try to broadcast our program in JavaScript:
Done! The script turned the workers, but slabochitaemym therefore generated 14 KB, we shall not give here.
Now it is worth remembering that Dart is designed primarily as a language that is executed in the browser. If we create a new project, while selecting the type of Wep Application, as a blank for the application we received the following dart-code:
We will not touch it, and notice that IDE, except for the script file, and create another html-template. And it is worth considering:
Then immediately struck by the presence of a new Internet Media Type - application / dart.Himself dart-script is loaded and executed by the <script> tag specified data type. After starting the application into the IDE will open the browser, and we can see our program of work:
Now let's look at the line
This is a common JavaScript-script that checks the browser DOM support functions
navigator.webkitStartDart () and in the absence of such substitutes dart-JavaScript-script files generated dark2js. So that the resulting page will work in Firefox (though not very fast).
How does Dart, we understand, now let's see what the language itself.
Introducing the Dart
We emphasize once again, Dart - it is not a dialect of JavaScript and not an add-on it is totally independent language, having c extension EcmaScript 262 is only one intersection - scope.Create it people seem to know and love technology C ++, Java and C #, and did not want to give up their habits. And it's great, but a little unusual for a traditional client-to-site-web programming. Here admire the Hello World on Dart`ski:
First of all struck by the use of the function
main (). She Dart does exactly that, and C / C ++, - serves as the entry point to an executable program, which begins its work. When the script is executed on the page, it begins to be executed immediately after loading DOM-document model. JavaSript - language with dynamic typing, that's great. In C ++, or Java strict typing, static, that in general is wonderful. But best of all this things at Dart - typing his optional. This means that when you declare variables, you can refer to types, but you can not do it all depends on the objectives of:
Keyword
final use in the case where the value to which it is initialized, there should be further modified. Annotation types can also occur in the ads function parameters and return values, and even to a certain class.OOP - honor the tradition!
Dart creators did not experiment with the OPP. The Dart classes and objects are used quite traditional within the framework of the classical model of inheritance:
An example of a simple class and one heir. There is, what to look for.
Dart supports short syntax update functions and methods.
Private members are designated by the addition of an underscore _ at the beginning of the name (in Dart no keywords public,
protected or private - if the name begins with an underscore (_), it is private property). The keyword this is used strictly traditional way of referring to a particular instance of the class.
Inheritance is supported by one class and multiple interfaces.
Using classes is also quite traditional:
An interesting feature classes in Dart is that each of them is implicit interface that you can implement. That is, to rewrite it with their own methods of implementation:
Here we implementiruem class interface in three different ways, but the function
printPerimetr () , waiting for a typed parameter (Shape), it works out successfully.
There Dart and abstract classes that can be used to implement a factory Designer:
Scope and libraries
Functions and classes can be organized in the library - a set of source files, grouped on the basis of logical considerations. Each library may comprise several classes ifunktsy upper layer. Libraries can import other necessary for work files and libraries. An example of a simple library is as follows:
And its use:
The keyword
library is used to define a library. Imports library using the keyword import, and part is used to refer to other source files. It is at the level of the library are as private members of the class. Customers library can not contact them directly, instead use accessor. Probably will not be a surprise the fact chtobibliotek to Dart has written a great many, and the main ones are supplied with the language as part of the Dart SDK. These we can observe (and, of course, be used) in the IDE Dart Editor. One of the most important are the dart: html - library for DOM-model browser and dart: io, which contains classes and functions for access to the file system, network sockets, for use with the HTTP protocol and web sockets.Insulators
Any Dart-application is running in single-threaded mode. But modern browsers, even on mobile platforms, working on multi-core processors. To use all the cores working simultaneously developers use threads with shared memory.
The Dart is a unit of work insulator
(isolate), and not the process or thread. Each insulator has its own memory area (this insulator mainly differs from the stream), inaccessible to any other insulator. Insulators can work in parallel, and exchange messages (signals and data).
The mechanism of insulators available on the web page - each script containing the function
main (), executed in a separate facility. When broadcast in JavaScript such constructions will be realized through technology HTML5 (WebWorkers).
Dart program can create a new detention center (similar to
fork ()). To create a new insulator with said entry point, you must import the library dart: isolate and call the spawnFunction (),passing it the name of the entry point:
Insulators good use for the distribution of logic between the processors and to dynamically load external code. The code is not part of the application can be downloaded iispolnyat in an isolated area.
Here, even the brief does not list all the features of the new technology of Google, but we are sure you can appreciate the scale of innovation. Dart clearly claims to be the universal and comprehensive web programming language.
Will it take him for his? The issue is complex. But the technology is very impressive start.
In general, ...
Honestly spending time on research about various developments JavaScript and presents the results in these pages mezhem not resist and did not notice that the idea to replace the JavaScript is not looks reasonable. Simple enough, yet powerful language has proved its right to exist, and some claim to it really seem far-fetched.
Комментариев нет:
Отправить комментарий