-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 17 additions & 26 deletions
43
packages/playground-examples/copy/zh/JavaScript/Modern JavaScript/Immutability.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,48 @@ | ||
// JavaScript is a language with a few ways to declare that | ||
// some of your objects don't change. The most prominent is | ||
// const - which says that the value won't change. | ||
// JavaScript 是有一门多种方式声明您的对象不可变的语言。最常见的方式是 | ||
// const ——表示某些值不会变。 | ||
|
||
const helloWorld = "Hello World"; | ||
|
||
// You cannot change helloWorld now, TypeScript will give | ||
// you an error about this, because you would get one at | ||
// runtime instead. | ||
// 您不可以更改 helloWorld,TypeScript 将会在您试图修改时报错, | ||
// 因为您同样不能再运行时这样做。 | ||
|
||
helloWorld = "Hi world"; | ||
|
||
// Why care about immutability? A lot of this is about | ||
// reducing complexity in your code. If you can reduce the | ||
// number of things which can change, then there are less | ||
// things to keep track of. | ||
// 为什么要关心不变性?很大一部分原因是为了减少代码复杂度。如果您 | ||
// 可以减少可变事物的数量,那么需要跟踪的东西也会随之变少。 | ||
|
||
// Using const is a great first step, however this fails | ||
// down a bit when using objects. | ||
// 使用 const 是很好的第一步,但是使用 object 时这样会失效。 | ||
|
||
const myConstantObject = { | ||
msg: "Hello World" | ||
}; | ||
|
||
// myConstantObject is not quite a constant though, because | ||
// we can still make changes to parts of the object, for | ||
// example we can change msg: | ||
// myConstantObject 并不是一个常量,因为我们仍然可以更改对象的某些 | ||
// 部分。例如我们可以更改 msg: | ||
|
||
myConstantObject.msg = "Hi World"; | ||
|
||
// const means the value at that point stays the same, but | ||
// that the object itself may change internally. This can | ||
// be changed using Object.freeze. | ||
// const 代表该引用的值不变,但对象本身可能在内部发生变化。 | ||
// 我们可以改用 Object.freeze 来实现。 | ||
|
||
const myDefinitelyConstantObject = Object.freeze({ | ||
msg: "Hello World" | ||
}); | ||
|
||
// When an object is frozen, then you cannot change the | ||
// internals. TypeScript will offer errors in these cases: | ||
// 当一个对象被 “冻结”,之后您将不能改变其内部。TypeScript 将 | ||
// 对这些情况抛出错误: | ||
|
||
myDefinitelyConstantObject.msg = "Hi World"; | ||
|
||
// This works the same for arrays too: | ||
// 这对数组同样有效: | ||
|
||
const myFrozenArray = Object.freeze(["Hi"]); | ||
myFrozenArray.push("World"); | ||
|
||
// Using freeze means you can trust that the object is | ||
// staying the same under the hood. | ||
// 使用 freeze 代表您确认这个对象将会保持不变。 | ||
|
||
// TypeScript has a few extra syntax hooks to improve working | ||
// with immutable data which you can find in the TypeScript | ||
// section of the examples: | ||
// TypeScript 有一些为了改进对不可变数据处理的额外语法,您可以 | ||
// 在这些例子中里看到他们: | ||
// | ||
// example:literals | ||
// example:type-widening-narrowing |
99 changes: 38 additions & 61 deletions
99
packages/playground-examples/copy/zh/JavaScript/Modern JavaScript/Import Export.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,86 @@ | ||
//// { order: 1, target: "ES5" } | ||
|
||
// JavaScript added import/export to the language back in 2016 | ||
// and TypeScript has complete support for this style of | ||
// linking between files and to external modules. TypeScript | ||
// expands on this syntax by also allowing types to be passed | ||
// with code. | ||
// JavaScript 于 2016 将 import、export 添加到该语言,TypeScript 完全 | ||
// 支持这种在文件和外部模块之间进行连接的风格。TypeScript 还通过允许将 | ||
// 类型与代码一起传递来扩展该语法。 | ||
|
||
// Let's look at importing code from a module. | ||
// 让我们看一下从模块导入代码。 | ||
|
||
import { danger, message, warn, DangerDSLType } from "danger"; | ||
|
||
// This takes a set of named imports from a node module | ||
// called danger. While there are more than four imports, | ||
// these are the only ones that we have chosen to import. | ||
// 这个例子需要从一个名为 “danger” 的 node 模块获取一组具名导入(named import)。 | ||
// 虽然有 4 个以上的导入,但是这是我们唯一选择导入的东西。 | ||
|
||
// Specifically naming which imports you are importing | ||
// gives tools the ability to remove unused code in your | ||
// apps, and helps you understand what is being used in | ||
// a particular file. | ||
// 指定您要导入的东西的名字可以帮助工具删除您的应用中未使用到的代码。 | ||
// 并且可以帮助您了解特定文件中正在使用的内容。 | ||
|
||
// In this case: danger, message and warn are JavaScript | ||
// imports - where as DangerDSLType is an interface type. | ||
// 在这个例子中,danger, message,和 warn 是 JavaScript 的 | ||
// 导入——DangerDSLType 是一个接口类型。 | ||
|
||
// TypeScript lets engineers document their code using | ||
// JSDoc, and docs are imported also. For example if | ||
// you hover on the different parts below, you see | ||
// explanations of what they are. | ||
// TypeScript 允许工程师使用 JSDoc 为他们的代码提供文档,并且 | ||
// 文档将被一起导入。例如您将鼠标悬停在下面不同的部分上,则 | ||
// 可以看到他们相关的解释。 | ||
|
||
danger.git.modified_files; | ||
|
||
// If you want to know how to provide these documentation | ||
// annotations read example:jsdoc-support | ||
// 如果您想了解怎么提供这些文档注解,可以查看 example:jsdoc-support | ||
|
||
// Another way to import code is by using the default export | ||
// of a module. An example of this is the debug module, which | ||
// exposes a function that creates a logging function. | ||
// 另一个导入代码的方式是让模块使用默认导出(default export)。debug 模块 | ||
// 是一个相关的例子,它导出了一个创建日志记录的函数。 | ||
|
||
import debug from "debug"; | ||
const log = debug("playground"); | ||
log("Started running code"); | ||
|
||
// Because of the nature of default exports having no true | ||
// name, they can be tricky when applied with static analysis | ||
// tools like the refactoring support in TypeScript but they | ||
// have their uses. | ||
// 由于默认导出不具有真实的名称,因此将他们与静态分析工具(如 TypeScript | ||
// 中的重构支持)一起使用时,他们可能会很棘手。但是他们有适合的用途。 | ||
|
||
// Because there is a long history in importing/exporting code | ||
// in JavaScript, there is a confusing part of default exports: | ||
// Some exports have documentation that implies you can write | ||
// an import like this: | ||
// 由于导入导出在 JavaScript 中历史悠久,所以会有一些关于默认导出的困惑: | ||
// 一些导出文件中的文档暗示您可以写这样形式的导入: | ||
|
||
import req from "request"; | ||
|
||
// However that fails, and then you find a stack overflow | ||
// which recommends the import as: | ||
// 但是这会报错,在 StackOverflow 上可以找到将其改为 import as 的建议: | ||
|
||
import * as req from "request"; | ||
|
||
// And this works. Why? We'll get back to that at the end of | ||
// our section on exporting. | ||
// 这样就可以正常工作了。为什么呢?我们将在导出部分的结尾回顾这一点。 | ||
|
||
// In order to import, you must be able to export. The modern | ||
// way to write exports is using the export keyword. | ||
// 为了可以导入,您必须先导出。现代的导出方式是使用 export 关键字。 | ||
|
||
/** The current stickers left on the roll */ | ||
/** 当前卷轴上的剩余的贴纸 */ | ||
export const numberOfStickers = 11; | ||
|
||
// This could be imported into another file by: | ||
// 它可以通过以如下形式被导入至另一个文件: | ||
// | ||
// import { numberOfStickers } from "./path/to/file" | ||
|
||
// You can have as many of those in a file as you like. Then | ||
// a default export is close to the same thing. | ||
// 您可以在一个文件中按需包含任意多个导出,默认导出几乎相与之同。 | ||
|
||
/** Generates a sticker for you */ | ||
const stickerGenerator = () => {}; | ||
/** 为您生成一个贴纸 */ | ||
const stickerGenerator = () => { }; | ||
export default stickerGenerator; | ||
|
||
// This could be imported into another file by: | ||
// 它可以以如下形式被导入至另一个文件: | ||
// | ||
// import getStickers from "./path/to/file" | ||
// | ||
// The naming is up to the module consumer. | ||
// 命名取决于模块的使用者。 | ||
|
||
// These aren't the only types of imports, just the most common | ||
// in modern code. Covering all of the ways code can cross | ||
// module boundaries is a very long topic in the handbook: | ||
// 导入的类型有很多,这里只介绍了现代代码中最常见的集中。手册中涵盖了 | ||
// 跨越模块便捷的所有方式,这是一个非常大的主题: | ||
// | ||
// https://www.typescriptlang.org/docs/handbook/modules.html | ||
|
||
// However, to try cover that last question. If you look at | ||
// the JavaScript code for this example - you'll see this: | ||
// 但是,为了解决上一个问题,如果查看此示例的 JavaScript 代码,您将看到: | ||
|
||
// var stickerGenerator = function () { }; | ||
// exports.default = stickerGenerator; | ||
|
||
// This sets the default property on the exports object | ||
// to be stickerGenerator. There is code out there which | ||
// sets exports to be a function, instead of an object. | ||
// 这会将 exports 对象的 default 属性设置为 stickerGenerator, | ||
// 这里的代码将 exports 设置为一个函数,而不是一个对象。 | ||
// | ||
// TypeScript opted to stick with the ECMAScript specification | ||
// about how to handle those cases, which is to raise an | ||
// error. However, there is a compiler setting which will | ||
// automatically handle those cases for you which is | ||
// esModuleInterop. | ||
// TypeScript 选择了 ECMAScript 规范中如何处理这些情况的部分,他们会 | ||
// 抛出一个错误。但是有一个编译器选项可以自动为您处理这些情况,即 esModuleInterop。 | ||
// | ||
// If you turn that on for this example, you will see that | ||
// error go away. | ||
// 如果您在示例中将此选项打开,您会看到错误消失了。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters