node deno_为什么Deno很棒

node deno

You’ve probably heard of Deno, the supposed legendary new Javascript runtime that allegedly solves many of the inherent problems with node. Created by Ryan Dahl, the maker of NodeJS, Deno includes various features that make developers' lives simpler.

您可能已经听说过Deno,它据说是传说中的新Javascript运行时,据称可以解决节点的许多固有问题。 由NodeJS的制造商Ryan Dahl创建的Deno包含使开发人员的生活更加轻松的各种功能。

Like most JS developers, the first thing that I thought when hearing about another JS framework was fear and preparation for a painful process of learning a new technology, but Deno has been surprisingly awesome for developing modern and fast JavaScript code.

像大多数JS开发人员一样,当我听到另一个JS框架时,我想到的第一件事就是恐惧和为学习新技术的痛苦过程做的准备,但是Deno在开发现代而快速JavaScript代码方面出奇的出色。

Let's take a look at why Deno is so attractive to developers in 2020.

让我们看一下为什么Deno在2020年对开发者如此有吸引力。

依赖现代JS导入语法 (Reliance on Modern JS Import Syntax)

Back when Node was created in 2009, the module import syntax relied on the require method. Modern Javascript uses the import syntax. For example, let's take a look at this code snippet:

早在2009年创建Node时,模块导入语法就依赖于require方法。 现代Javascript使用import语法。 例如,让我们看一下以下代码片段:

// Traditional JS Method
const module = require('module');
// ES6 Module Method
import { module } from 'module';

If you work with modern frameworks like React or Angular, you’re probably using the ES6 module syntax. Deno uses the ES6 module syntax by default.

如果您使用React或Angular等现代框架,则可能正在使用ES6模块语法。 默认情况下,Deno使用ES6模块语法。

Why ES6 Module Import Syntax is Better

为什么ES6模块导入语法更好

  1. With import, you can selectively load in modules from a package, saving memory

    使用import ,您可以有选择地从包中加载模块,从而节省内存

  2. With require, loading is synchronous (meaning it happens in the process foreground), with import loading is asynchronous, which drastically improves performance when importing modules.

    使用require ,加载是同步的(这意味着它发生在进程前台),而import加载是异步的,这可以大大提高导入模块时的性能。

分散包装 (Decentralized Packages)

With NodeJS, you’re probably used to using NPM to keep track of and load modules using a package.json. Whenever you want to use an external package, you have to first install the package:

使用NodeJS,您可能已经习惯使用NPM来使用package.json跟踪和加载模块。 每当您要使用外部软件包时,都必须先安装该软件包:

npm i package

Then import it:

然后将其导入:

const moment = require("moment")

Whenever someone wants to run your package locally, they would have to install all the packages separately. If you’re running multiple projects on your machine that rely on the same packages, there’s no easy way to share packages between projects, so duplicate packages will be installed on your machine, wasting space.

每当有人要在本地运行您的软件包时,他们就必须分别安装所有软件包。 如果您在机器上运行依赖同一软件包的多个项目,则没有简单的方法可以在项目之间共享软件包,因此重复的软件包将安装在您的计算机上,从而浪费了空间。

In Deno, packages are imported from a URL:

在Deno中,包是从URL导入的:

import { moment } from 'https://deno.land/x/moment/moment.ts.'

Deno automatically caches packages on your machine after installation, so packages are only installed once.

安装后,Deno会自动在您的计算机上缓存软件包,因此软件包仅安装一次

本机TypeScript (Native TypeScript)

If you don’t know what TypeScript is, you probably should do some reading on it here. Normally, in Node getting TypeScript to work is a multistep process. You’d have to install typescript, update the package.json, tsconfig.json, and make sure your modules have @types supported.

如果您不知道TypeScript是什么,那么您可能应该在这里进行一些阅读。 通常,在Node中让TypeScript运行是一个多步骤的过程。 您必须安装打字稿,更新package.jsontsconfig.json ,并确保您的模块支持@types。

With Deno, TypeScript support is natively baked in!

有了Deno, 就可以原生支持TypeScript!

顶级等待 (Top Level Await)

In Node, the await keyword can only be used in async functions:

在Node中, await关键字只能在异步函数中使用:

const getData = async () => {
const data = await fetch('https://google.com');
const result = await data.json();
}

With Deno, you can use await anywhere, including top level code, so you don’t have to declare an async function before using await!

使用Deno,您可以在任何地方使用await,包括顶级代码,因此您不必在使用await之前声明异步函数!

// No Async Needed!
const data = await fetch('https://google.com');
const result = await data.json();

This is a drastic improvement that makes code simpler and easier to write!

这是一个巨大的改进,使代码更容易编写!

访问浏览器API (Access to the Browser API)

Using the Browser API, which includes access to methods like fetch, normally isn’t accessible by default, you have to install an NPM package.

使用浏览器API(包括访问诸如fetch之类的方法)通常默认情况下是无法访问的,因此必须安装NPM软件包。

Deno automatically has access to the Browser API, so you can call fetch without importing any other packages.

Deno自动访问浏览器API,因此您可以调用fetch而不导入任何其他软件包。

This makes code significantly simpler, and eliminates having to import additional modules.

这使代码大大简化,并且无需导入其他模块。

Image for post
Photo by Javier Allegue Barros on Unsplash
Javier Allegue BarrosUnsplash拍摄的照片

迪诺的未来 (Deno’s Future)

Deno has many other advantages besides these, far more than can be covered in this article.

除这些优点外,Deno还具有许多其他优点,远远超出了本文所涵盖的范围。

Combined, all these features make it easier to write clean, modern, and fast JavaScript code. As a React and Angular developer, the modern features and native TypeScript support of Deno are naturally appealing.

所有这些功能结合在一起,使编写干净,现代和快速JavaScript代码变得更加容易。 作为React和Angular开发人员,Deno的现代功能和对TypeType的本机支持自然吸引人。

Will Deno ever replace NodeJS? Probably not anytime soon. NodeJS is quite entrenched in the market, but more and more JavaScript developers are switching to Deno for their next project.

Deno会取代NodeJS吗? 大概不会很快。 NodeJS在市场上已经根深蒂固,但是越来越多JavaScript开发人员正在转向Deno进行下一个项目。

保持联系 (Keep in Touch)

There’s a lot of content out there, I appreciate you reading mine. I’m a young entrepreneur and I write about software development and my experience running companies. You can signup for my newsletter here

那里有很多内容,感谢您阅读我的内容。 我是一位年轻的企业家,我撰写有关软件开发和我经营公司的经验的文章。 您可以在这里注册我的时事通讯

Feel free to reach out and connect with me on Linkedin or Twitter.

请随时通过LinkedinTwitter与我联系。

翻译自: https://levelup.gitconnected.com/denos-top-features-9e387cd29530

node deno

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值