-
Notifications
You must be signed in to change notification settings - Fork 32
Translate ES6 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
e5395a0
bf25da2
3342d58
0a37e22
a05db5b
36a6787
e56a241
4267806
da09657
d080545
e42f5cf
e22a645
2681e72
589ce65
ef148e5
2dad887
7ea3f14
5b08d48
8a9e928
99ce5ce
3bf748b
177b569
f90315b
37fba7c
91a8634
8cf8e94
4a711dc
588c816
3bd3606
60128e9
b0263b3
0cac235
b35427d
0fbaf7e
a1c16db
0c17ea8
38a70b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,33 +8,33 @@ | |
| "id": "587d7b87367417b2b2512b3f", | ||
| "title": "Explore Differences Between the var and let Keywords", | ||
| "description": [ | ||
| "One of the biggest problems with declaring variables with the <code>var</code> keyword is that you can overwrite variable declarations without an error.", | ||
| "使用 <code>var</code> 关键字来声明变量,会出现重复声明导致变量被覆盖却不会报错的问题。", | ||
| "<blockquote>var camper = 'James';<br>var camper = 'David';<br>console.log(camper);<br>// logs 'David'</blockquote>", | ||
|
||
| "As you can see in the code above, the <code>camper</code> variable is originally declared as <code>James</code> and then overridden to be <code>David</code>.", | ||
| "In a small application, you might not run into this type of problem, but when your code becomes larger, you might accidentally overwrite a variable that you did not intend to overwrite.", | ||
| "Because this behavior does not throw an error, searching and fixing bugs becomes more difficult.<br>", | ||
| "A new keyword called <code>let</code> was introduced in ES6 to solve this potential issue with the <code>var</code> keyword.", | ||
| "If you were to replace <code>var</code> with <code>let</code> in the variable declarations of the code above, the result would be an error.", | ||
| "看上面的代码, <code>camper</code> 最初声明为 <code>James</code>,然后又被覆盖成了 <code>David</code>。", | ||
|
||
| "在小型的应用中,你可能不会遇到这样的问题,但是当你的代码规模变得更加庞大的时候,就可能会在你不经意的时候产生变量的覆盖", | ||
|
||
| "因为这样的行为并不会报错,导致 debug 变得更加困难。<br>", | ||
|
||
| "在 ES6 中使用了新的关键字 <code>let</code> 来解决 <code>var</code> 关键字可能带来的问题。", | ||
|
||
| "如果你在上面的代码中,使用了 <code>let</code> 关键字来代替 <code>var</code>关键字,结果会是一个报错。", | ||
|
||
| "<blockquote>let camper = 'James';<br>let camper = 'David'; // throws an error</blockquote>", | ||
| "This error can be seen in the console of your browser.", | ||
| "So unlike <code>var</code>, when using <code>let</code>, a variable with the same name can only be declared once.", | ||
| "Note the <code>\"use strict\"</code>. This enables Strict Mode, which catches common coding mistakes and \"unsafe\" actions. For instance:", | ||
| "可以在可以在浏览器的控制台里看见这个错误。", | ||
|
||
| "与 <code>var</code> 不同的是, 当使用 <code>let</code> 的时候,同一名字的变量只能被声明一次。", | ||
| "请注意 <code>\"use strict\"</code>。这代表着开启了严格模式, 用于检测常见的代码错误以及\"不安全\"的行为,例如:", | ||
| "<blockquote>\"use strict\";<br>x = 3.14; // throws an error because x is not declared</blockquote>", | ||
| "<hr>", | ||
| "Update the code so it only uses the <code>let</code> keyword." | ||
| "请更新这段代码,并且在其中只使用 <code>let</code> 关键字" | ||
| ], | ||
| "tests": [ | ||
| { | ||
| "text": "<code>var</code> does not exist in code.", | ||
| "testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> does not exist in code.');" | ||
| "text": "在代码中不应存在 <code>var</code>。", | ||
| "testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'在代码中不应存在 <code>var</code>。');" | ||
| }, | ||
| { | ||
| "text": "<code>catName</code> should be <code>Oliver</code>.", | ||
| "testString": "assert(catName === \"Oliver\", '<code>catName</code> should be <code>Oliver</code>.');" | ||
| "text": "<code>catName</code> 变量的值应该为 <code>Oliver</code>。", | ||
|
||
| "testString": "assert(catName === \"Oliver\", '<code><code>catName</code> 变量的值应该为 <code>\"Oliver\"</code>。');" | ||
| }, | ||
| { | ||
| "text": "<code>quote</code> should be <code>\"Oliver says Meow!\"</code>", | ||
| "testString": "assert(quote === \"Oliver says Meow!\", '<code>quote</code> should be <code>\"Oliver says Meow!\"</code>');" | ||
| "text": "<code>quote</code> 变量的值应该为 <code>\"Oliver says Meow!\"</code>", | ||
| "testString": "assert(quote === \"Oliver says Meow!\", '<code>quote</code> 变量的值应该为 <code>\"Oliver says Meow!\"</code>');" | ||
| } | ||
| ], | ||
| "releasedOn": "Feb 17, 2017", | ||
|
|
@@ -65,33 +65,33 @@ | |
| "id": "587d7b87367417b2b2512b40", | ||
| "title": "Compare Scopes of the var and let Keywords", | ||
| "description": [ | ||
| "When you declare a variable with the <code>var</code> keyword, it is declared globally, or locally if declared inside a function.", | ||
| "The <code>let</code> keyword behaves similarly, but with some extra features. When you declare a variable with the <code>let</code> keyword inside a block, statement, or expression, its scope is limited to that block, statement, or expression.", | ||
| "For example:", | ||
| "当你使用 <code>var</code> 关键字来声明一个变量的时候,这个变量会被声明成全局变量,或是函数内的局部变量。", | ||
| "<code>let</code> 关键字的作用类似,但会有一些额外的特性。 当你使用 <code>let</code> 关键字在代码块,语句或者表达式里声明变量的时候,这个变量的作用域就在当前的代码块,语句或表达式之中。", | ||
|
||
| "举个例子:", | ||
| "<blockquote>var numArray = [];<br>for (var i = 0; i < 3; i++) {<br> numArray.push(i);<br>}<br>console.log(numArray);<br>// returns [0, 1, 2]<br>console.log(i);<br>// returns 3</blockquote>", | ||
|
||
| "With the <code>var</code> keyword, <code>i</code> is declared globally. So when <code>i++</code> is executed, it updates the global variable. This code is similar to the following:", | ||
| "当使用 <code>var</code> 关键字的时候, <code>i</code> 会被声明成全局变量。 当 <code>i++</code> 执行的时候, 它会改变全局变量的值。 这段代码可以看做下面这样:", | ||
| "<blockquote>var numArray = [];<br>var i;<br>for (i = 0; i < 3; i++) {<br> numArray.push(i);<br>}<br>console.log(numArray);<br>// returns [0, 1, 2]<br>console.log(i);<br>// returns 3</blockquote>", | ||
| "This behavior will cause problems if you were to create a function and store it for later use inside a for loop that uses the <code>i</code> variable. This is because the stored function will always refer to the value of the updated global <code>i</code> variable.", | ||
| "这个行为在你一个创建函数用来存储在 for 循环中使用的 <code>i</code> 变量的时候,会出现问题。这是因为函数存储的值会因为全局变量 <code>i</code>的变化而不断的改变。", | ||
|
||
| "<blockquote>var printNumTwo;<br>for (var i = 0; i < 3; i++) {<br> if(i === 2){<br> printNumTwo = function() {<br> return i;<br> };<br> }<br>}<br>console.log(printNumTwo());<br>// returns 3</blockquote>", | ||
| "As you can see, <code>printNumTwo()</code> prints 3 and not 2. This is because the value assigned to <code>i</code> was updated and the <code>printNumTwo()</code> returns the global <code>i</code> and not the value <code>i</code> had when the function was created in the for loop. The <code>let</code> keyword does not follow this behavior:", | ||
| "可以看到, <code>printNumTwo()</code> 打印了 3 而不是 2。 这是因为 <code>i</code> 发生了改变,并且函数 <code>printNumTwo()</code> 返回的是全局变量 <code>i</code>的值,而不是 for 循环中创建函数时 <code>i</code> 的值。The <code>let</code> 关键字就不会有这种现象:", | ||
|
||
| "<blockquote>'use strict';<br>let printNumTwo;<br>for (let i = 0; i < 3; i++) {<br> if (i === 2) {<br> printNumTwo = function() {<br> return i;<br> };<br> }<br>}<br>console.log(printNumTwo());<br>// returns 2<br>console.log(i);<br>// returns \"i is not defined\"</blockquote>", | ||
| "<code>i</code> is not defined because it was not declared in the global scope. It is only declared within the for loop statement. <code>printNumTwo()</code> returned the correct value because three different <code>i</code> variables with unique values (0, 1, and 2) were created by the <code>let</code> keyword within the loop statement.", | ||
| "<code>i</code> 在全局作用域中没有声明,所以它没有被定义, 它只会在 for 循环的语句中被声明。 因为在循环语句中的 <code>let</code>关键字创建了拥有独一无二的值 (0, 1, 或 2) 的三个不同 <code>i</code> 变量,所以 <code>printNumTwo()</code> 返回了正确的值。", | ||
|
||
| "<hr>", | ||
| "Fix the code so that <code>i</code> declared in the if statement is a separate variable than <code>i</code> declared in the first line of the function. Be certain not to use the <code>var</code> keyword anywhere in your code.", | ||
| "This exercise is designed to illustrate the difference between how <code>var</code> and <code>let</code> keywords assign scope to the declared variable. When programming a function similar to the one used in this exercise, it is often better to use different variable names to avoid confusion." | ||
| "修改这段代码,使得在 if 语句中声明的 <code>i</code> 变量与在函数的第一行声明的 <code>i</code> 变量是彼此独立的。 请注意不要在你的代码的任何地方使用 <code>var</code> 关键字。", | ||
| "这个练习说明了使用 <code>var</code> 与 <code>let</code>关键字声明变量时,作用域之间的不同。当编写类似这个练习中的函数的时候,通常来说最好还是使用不同的变量名,用于避免困惑。" | ||
|
||
| ], | ||
| "tests": [ | ||
| { | ||
| "text": "<code>var</code> does not exist in code.", | ||
| "testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> does not exist in code.');" | ||
| "text": "<code>var</code> 不应该在代码中存在。", | ||
| "testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> 不应该在代码中存在。');" | ||
| }, | ||
| { | ||
| "text": "The variable <code>i</code> declared in the if statement should equal \"block scope\".", | ||
| "testString": "getUserInput => assert(getUserInput('index').match(/(i\\s*=\\s*).*\\s*.*\\s*.*\\1('|\")block\\s*scope\\2/g), 'The variable <code>i</code> declared in the if statement should equal \"block scope\".');" | ||
| "text": "在 if 语句中声明的 <code>i</code> 变量的值是 \"block scope\"。", | ||
|
||
| "testString": "getUserInput => assert(getUserInput('index').match(/(i\\s*=\\s*).*\\s*.*\\s*.*\\1('|\")block\\s*scope\\2/g), '在 if 语句中声明的 <code>i</code> 变量应该是 \"block scope\"。');" | ||
| }, | ||
| { | ||
| "text": "<code>checkScope()</code> should return \"function scope\"", | ||
| "testString": "assert(checkScope() === \"function scope\", '<code>checkScope()</code> should return \"function scope\"');" | ||
| "text": "<code>checkScope()</code> 应当 返回 \"function scope\"", | ||
|
||
| "testString": "assert(checkScope() === \"function scope\", '<code>checkScope()</code> 应该返回 \"function scope\"');" | ||
| } | ||
| ], | ||
| "releasedOn": "Feb 17, 2017", | ||
|
|
@@ -1362,4 +1362,4 @@ | |
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
请把逗号改成全角的。下同
另外,这一行是引出下文的,建议结尾用冒号
: