We all know that JavaScript is not the best language to achieve all tasks. When encountering complex applications, it may not be suitable. To avoid this problem, several new languages and variations of existing languages have been created, all of which generate code that can run in the browser without having to write any JavaScript code or taking into account the limitations of the language.
Dart is a classic object-oriented language in which everything is an object and any object is an instance of a class (objects can also act as functions). It is designed specifically for building applications for browsers, servers, and mobile devices. It is maintained by Google and is the language driving the next generation user interface for AdWords, the most important product that generates revenue for Google, which in itself is a testament to its power at scale.
The language can be translated into JavaScript for use in the browser, or interpreted and executed directly by the Dart VM, which also allows you to build server applications. Mobile apps can be created using the Flutter SDK.
Complex applications also require a mature set of libraries and language features designed specifically for the task, and Dart includes all of them. An example of a popular library is AngularDart, the Dart version of Angular.
It allows you to write type-safe code without being too intrusive; you can write types, but you don't need to because they can be inferred. This allows for creating quick prototypes without having to overthink the details, but once your prototype works you can add types to make it more robust.
Regarding concurrent programming in virtual machines, Dart uses so-called Isolates along with their own memory heaps, instead of shared memory threads (Dart is single-threaded), using messages to implement communication. In the browser, this event is slightly different: instead of creating new isolates, you create new Workers .
// Example extracted from dartlang.org import 'dart:async'; import 'dart:math' show Random; main() async { print('Compute π using the Monte Carlo method.'); await for (var estimate in computePi()) { print('π ≅ $estimate'); } } /// Generates a stream of increasingly accurate estimates of π. Stream<double> computePi({int batch: 1000000}) async* { var total = 0; var count = 0; while (true) { var points = generateRandom().take(batch); var inside = points.where((p) => p.isInsideUnitCircle); total += batch; count += inside.length; var ratio = count / total; // Area of a circle is A = π⋅r², therefore π = A/r². // So, when given random points with x ∈ <0,1>, // y ∈ <0,1>, the ratio of those inside a unit circle // should approach π / 4. Therefore, the value of π // should be: yield ratio * 4; } } Iterable<Point> generateRandom([int seed]) sync* { final random = new Random(seed); while (true) { yield new Point(random.nextDouble(), random.nextDouble()); } } class Point { final double x, y; const Point(this.x, this.y); bool get isInsideUnitCircle => x * x + y * y <= 1; }
Getting started with Dart
TypeScript is a superset of JavaScript; a valid JavaScript program is also a valid TypeScript program, but The latter adds static typing. Its compiler can also act as a translator from ES2015+ to the current implementation, so you always get the latest features.
Unlike many other languages, TypeScript keeps the spirit of JavaScript intact and only adds features to make your code more reliable. These are type annotations, along with other type-related features, that make writing JavaScript more enjoyable, thanks to enabling specialized tools like static analyzers to aid in the refactoring process. Additionally, the addition of types improves the interface between different components of the application.
Type inference is supported so you don't have to write all types from the beginning. You can write quick solutions and then add all the types to gain confidence in your code.
TypeScript also supports advanced types such as intersection types, union types, type aliases, discriminated unions, and type guards. You can view all of these types in the Advanced Types page of the TypeScript documentation site.
If you use React, you can also support JSX by adding React types.
class Person { private name: string; private age: number; private salary: number; constructor(name: string, age: number, salary: number) { this.name = name; this.age = age; this.salary = salary; } toString(): string { return `${this.name} (${this.age}) (${this.salary})`; } }
Elm is a purely functional programming language that compiles to JS, HTML and CSS. You can build an entire site using just Elm. This makes it a great alternative to JavaScript frameworks like React. Applications built with it automatically use the virtual DOM library, making them very fast. One of its big advantages is the built-in architecture, which makes you forget about data flow and focus on data declaration and logic.
In Elm, all functions are pure, which means they will always return the same output for a given input. They can't do anything else unless you specify it. For example, to access a remote API, you would create a command (command) function to communicate with the outside world, and a subscription (subscription) to listen for responses. Another point of purity is that values are immutable; when you need something, you create new values rather than modify old ones.
Elm adoption can be incremental; ports can be used to communicate with JavaScript and other libraries. Although Elm has not yet reached version 1, it is being used in complex and large applications, making it a viable solution for complex applications.
One of the most attractive features of Elm is the beginner-friendly compiler that, instead of generating hard-to-read messages, generates code that can help you fix your code. If you're learning the language, the compiler itself can be of great help.
module Main exposing (..) import Html exposing (..) -- MAIN main : Program Never Model Msg main = Html.program { init = init , update = update , view = view , subscriptions = subscriptions } -- INIT type alias Model = String init : ( Model, Cmd Msg ) init = ( "Hello World!", Cmd.none ) -- UPDATE type Msg = DoNothing update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of DoNothing -> ( model, Cmd.none ) -- VIEW view : Model -> Html Msg view model = p [] [text model] -- SUBSCRIPTIONS subscriptions : Model -> Sub Msg subscriptions model = Sub.none
Get started with Elm
PureScript is a purely functional and strongly typed programming language created by Phil Freeman. It aims to provide strong compatibility with available JavaScript libraries, similar in spirit to Haskell, but keeping the core of JavaScript.
PureScript 的一个强项是它的极简主义。它不包括在其他语言中被认为是必需的功能的任何库。例如,不是编译器本身包含生成器和 promises,而是你可以使用特定的库来完成任务。你可以为所需功能选择想要的实现,这样可以在使用 PureScript 时实现高效和个性化的体验,同时保持生成的代码尽可能小。
其编译器的另一个显著特征就是能够在保持与 JavaScript 的兼容性的同时, 用库和工具生成整洁和可读的代码。
像其他语言一样,PureScript 有自己的构建工具叫做 Pulp,可以与 Gulp 进行比较, 但是用于以这种语言编写的项目。
关于类型系统,与 Elm不同,即另一种 ML 式的语言,PureScript 支持高级类型的功能,如取自 Haskell 的 higher-kinded types(高级类类型) 以及 type classes(类型类), 从而允许创建复杂的抽象。
module Main where import Prelude import Data.Foldable (fold) import TryPureScript main = render $ fold [ h1 (text "Try PureScript!") , p (text "Try out the examples below, or create your own!") , h2 (text "Examples") , list (map fromExample examples) ] where fromExample { title, gist } = link ("?gist=" <> gist) (text title) examples = [ { title: "Algebraic Data Types" , gist: "37c3c97f47a43f20c548" } , { title: "Loops" , gist: "cfdabdcd085d4ac3dc46" } , { title: "Operators" , gist: "3044550f29a7c5d3d0d0" } ]
开始使用 PureScript
CoffeeScript 是一种语言,旨在公开 JavaScript 的良好部分,同时提供更整洁的语法并保留语义。虽然该语言的流行度近年来一直在减弱,但它正在改变方向,现在正在获得一个新的主要版本,为 ES2015+ 的功能提供支持。
你用 CoffeeScript 编写的代码被直接翻译为可读的 JavaScript 代码,并保持与现有库的兼容性。从版本 2 开始,编译器将产生与最新版本的 ECMAScript 兼容的代码。例如,每次你使用一个类,你就获得一个 JavaScript 类。另外,如果你使用 React,也有好消息: JSX 与 CoffeeScript 兼容。
以上内容介绍了10个有趣的语言,这些语言都可以转换成JavaScript代码在浏览器中执行,也可以在Node.js这样的平台上执行。希望能帮助到大家。
相关推荐:
The above is the detailed content of 10 interesting JavaScript scripting languages. For more information, please follow other related articles on the PHP Chinese website!