-
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
93 additions
and
144 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
66 changes: 25 additions & 41 deletions
66
packages/playground-examples/copy/zh/JavaScript/JavaScript Essentials/Functions.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,89 +1,73 @@ | ||
//// { order: 2, compiler: { noImplicitAny: false } } | ||
|
||
// There are quite a few ways to declare a function in | ||
// JavaScript. Let's look at a function which adds two | ||
// numbers together: | ||
// 在 JavaScript 中有很多方式可以声明函数。让我们看一个将两个 | ||
// 数字相加的函数: | ||
|
||
// Creates a function in global scope called addOldSchool | ||
// 创建一个叫做 addOldSchool 的全局函数。 | ||
function addOldSchool(x, y) { | ||
return x + y; | ||
} | ||
|
||
// You can move the name of the function to a variable | ||
// name also | ||
const anonymousOldSchoolFunction = function(x, y) { | ||
// 您也可以将函数的名称移动到一个变量名中。 | ||
const anonymousOldSchoolFunction = function (x, y) { | ||
return x + y; | ||
}; | ||
|
||
// You can also use fat-arrow shorthand for a function | ||
// 您也可以使用箭头函数作为函数的快捷方式。 | ||
const addFunction = (x, y) => { | ||
return x + y; | ||
}; | ||
|
||
// We're going to focus on the last one, but everything | ||
// applies to all three formats. | ||
// 我们将着重介绍最后一种方式,但是所有内容都适用于全部三种方式。 | ||
|
||
// TypeScript provides additional syntax which adds to a | ||
// function definition and offers hints on what types | ||
// are expected by this function. | ||
// TypeScript 提供了函数定义和标记函数预期类型的额外语法。 | ||
// | ||
// Up next is the most open version of the add function, it | ||
// says that add takes two inputs of any type: this could | ||
// be strings, numbers or objects which you've made. | ||
// 接下来是 add 函数的最开放的版本,它代表 add 接受任意类型的两个 | ||
// 输入:它可以是您输入的字符串,数字或对象。 | ||
|
||
const add1 = (x: any, y: any) => { | ||
return x + y; | ||
}; | ||
add1("Hello", 23); | ||
|
||
// This is legitimate JavaScript (strings can be added | ||
// like this for example) but isn't optimal for our function | ||
// which we know is for numbers, so we'll convert the x and | ||
// y to only be numbers. | ||
// 这是合法的 JavaScript(例如可以这样连接字符串),但是对于我们 | ||
// 已知的适用于数字的函数并不是最佳选择。因此我们将 x 和 y 转换为 | ||
// 仅接受数字。 | ||
|
||
const add2 = (x: number, y: number) => { | ||
return x + y; | ||
}; | ||
add2(16, 23); | ||
add2("Hello", 23); | ||
|
||
// Great. We get an error when anything other than a number | ||
// is passed in. If you hover over the word add2 above, | ||
// you'll see that TypeScript describes it as: | ||
// 很好,如果我们传入任何其他非数字的东西可以得到一个错误。如果 | ||
// 您将鼠标悬停在上面的 add2 上,您将会看到 TypeScript 将其描述为: | ||
// | ||
// const add2: (x: number, y: number) => number | ||
// | ||
// Where it has inferred that when the two inputs are | ||
// numbers the only possible return type is a number. | ||
// This is great, you don't have to write extra syntax. | ||
// Let's look at what it takes to do that: | ||
// 推断出两个输入都是数字时,数字也是唯一可能的返回值类型。很好,您不 | ||
// 需要编写额外的语法。让我们看看这样做的必要条件: | ||
|
||
const add3 = (x: number, y: number): string => { | ||
return x + y; | ||
}; | ||
|
||
// This function fails because we told TypeScript that it | ||
// should expect a string to be returned but the function | ||
// didn't live up to that promise. | ||
// 这个函数将会报错,因为我们告诉 TypeScript 应该返回一个字符串, | ||
// 但是函数没有实现其承诺。 | ||
|
||
const add4 = (x: number, y: number): number => { | ||
return x + y; | ||
}; | ||
|
||
// This is a very explicit version of add2 - there are | ||
// cases when you want to use the explicit return type | ||
// syntax to give yourself a space to work within before | ||
// you get started. A bit like how test-driven development | ||
// recommends starting with a failing test, but in this case | ||
// it's with a failing shape of a function instead. | ||
// 这是非常明确的 add2 的版本——在某些情况下,您希望使用显式的返回类型 | ||
// 语法给自己在开始前留下一些工作空间。和测试驱动开发建议从失败的测试开始 | ||
// 类似。但是在这种情况下,它的函数是不正确的。 | ||
|
||
// This example is only a primer, you can learn a lot more | ||
// about how functions work in TypeScript in the handbook and | ||
// inside the Functional JavaScript section of the examples: | ||
// 此示例仅是入门知识,您可以在手册以及 JavaScript 函数式编程的示例中 | ||
// 了解更多关于 TypeScript 函数的工作原理: | ||
// | ||
// https://www.typescriptlang.org/docs/handbook/functions.html | ||
// example:function-chaining | ||
|
||
// And to continue our tour of JavaScript essentials, | ||
// we'll look at how code flow affects the TypeScript types: | ||
// 为了继续学习 JavaScript 精粹,我们将了解代码流如何影响 TypeScript 的类型: | ||
// example:code-flow |
32 changes: 13 additions & 19 deletions
32
packages/playground-examples/copy/zh/JavaScript/JavaScript Essentials/Hello World.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,34 +1,28 @@ | ||
//// { order: 0, compiler: { target: 1 } } | ||
|
||
// Welcome to the TypeScript playground. This site is a lot | ||
// like running a TypeScript project inside a web browser. | ||
// 欢迎来到 TypeScript 在线演示,本网站很像在浏览器中运行 | ||
// 一个 TypeScript 项目。 | ||
|
||
// The playground makes it easy for you to safely experiment | ||
// with ideas in TypeScript by making it trivial to share | ||
// these projects. The URL for this page is everything | ||
// required to load the project for someone else. | ||
// 通过分享这些项目,可以轻松并安全的通过在线演示在 TypeScript 中 | ||
// 尝试想法。此页面的 URL 是其他人加载本项目所需的全部内容。 | ||
|
||
const hello = "Hello" | ||
|
||
// You can see on the right the result of the TypeScript | ||
// compiler: this is vanilla JavaScript which can run on | ||
// browsers, servers or anywhere really. | ||
// 你可以在右侧看到 TypeScript 编译器的输出结果:这是可以在浏览器、 | ||
// 服务器或任何实际位置上运行的原始 JavaScript。 | ||
|
||
const world = "World" | ||
|
||
// You can see how it makes tiny changes to the code, by | ||
// converting a "const" to a "var". This is one of the many | ||
// things TypeScript does to make it possible to run | ||
// anywhere JavaScript runs. | ||
// 通过将 “const” 改为 “var”,您可以看到它如何对代码进行微小的更高。 | ||
// 这是使 TypeScript 为了可以运行在任何运行 JavaScript 的地方所 | ||
// 做的工作之一。 | ||
|
||
console.log(hello + " " + world) | ||
|
||
// Now that you have an idea of how the playground works, | ||
// let's look at how TypeScript makes working with | ||
// JavaScript more fun. During this section we'll be trying | ||
// to keep as close to vanilla JavaScript as possible to | ||
// show how you can re-use existing knowledge. | ||
// 现在您已经了解了 TypeScript 的工作原理,让我们了解一下 TypeScript | ||
// 如何使使用 JavaScript 更有趣。在本节中,我们将尽可能接近原生 JavaScript, | ||
// 已显示如何复用现有知识。 | ||
// | ||
// Click below to continue: | ||
// 点击下方继续: | ||
// | ||
// example:objects-and-arrays |
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