1-js/06-advanced-functions/07-new-function#77
Merged
leviding merged 2 commits intojavascript-tutorial:zh-hansfrom May 8, 2018
Merged
1-js/06-advanced-functions/07-new-function#77leviding merged 2 commits intojavascript-tutorial:zh-hansfrom
leviding merged 2 commits intojavascript-tutorial:zh-hansfrom
Conversation
Contributor
|
@leviding 校对认领 |
Member
|
@Starriers ok |
Starriers
reviewed
May 5, 2018
Contributor
Starriers
left a comment
There was a problem hiding this comment.
@lcx-seima @leviding 校对完成
| ``` | ||
|
|
||
| If there are no arguments, then there's only a single argument, the function body: | ||
| 如果创建出的新函数没有任何入参,那么创建函数时你只需要传入一个参数,即描述新函数函数体的字符串: |
| 与已知方法相比这种方法最大的不同是,它是在运行时使用描述函数的字符串来创建函数的。 | ||
|
|
||
| All previous declarations required us, programmers, to write the function code in the script. | ||
| 之前的各种声明方法都需要我们,程序员,在脚本中编写各个函数的代码。 |
| 通常,函数会使用一个特殊的属性 `[[Environment]]` 来记录函数创建时的环境,它具体指向了函数创建时的词法空间。 | ||
|
|
||
| But when a function is created using `new Function`, its `[[Environment]]` references not the current Lexical Environment, but instead the global one. | ||
| 但是如果我们使用 `new Function` 创建函数,函数的 `[[Environment]]` 并不指向当前的词法空间,而是指向全局空间。 |
| 我们还会遇到这种问题,当发布 JavaScript 代码到生产环境时,我们会使用 *minifier* 压缩代码 —— 这是一个特别的程序,它会移除代码中多余的注释、空格等以减小文件 —— 更重要的是,它会用更短的字符重命名所有的本地变量。 | ||
|
|
||
| For instance, if a function has `let userName`, minifier replaces it `let a` (or another letter if this one is occupied), and does it everywhere. That's usually a safe thing to do, because the variable is local, nothing outside the function can access it. And inside the function, minifier replaces every mention of it. Minifiers are smart, they analyze the code structure, so they don't break anything. They're not just a dumb find-and-replace. | ||
| 举个例子,如果一个函数包含了 `let userName`,minifier 会把它替换为 `let a`(如果 a 已被使用便换其他字符),剩余的本地变量也会做类似的替换。一般来说这样的替换是安全的,毕竟这些变量是函数内的本地变量,它们不能被函数以外的表达式访问。同时,minifier 会替换函数内部所有使用了变量的代码。minifier 很聪明,它会分析代码的结构,而不是呆板地查找然后替换,因此它不会“破坏”你的程序。 |
| ``` | ||
|
|
||
| Functions created with `new Function`, have `[[Environment]]` referencing the global Lexical Environment, not the outer one. Hence, they cannot use outer variables. But that's actually good, because it saves us from errors. Passing parameters explicitly is a much better method architecturally and causes no problems with minifiers. | ||
| 使用 `new Function` 创建出来的函数,它的 `[[Environment]]` 指向全局词法空间,而不是函数所在的外部词法空间。因此,我们不能在新函数中直接使用外部变量。不过这样也挺好,这有助于减少我们代码中可能出现的错误。同时使用参数显式地传值也有助于维护良好的代码结构且避免了因使用 minifier 带来的问题。 |
| 也许我们还希望新函数能够访问到外部作用域的变量? | ||
|
|
||
| The problem is that before JavaScript is published to production, it's compressed using a *minifier* -- a special program that shrinks code by removing extra comments, spaces and -- what's important, renames local variables into shorter ones. | ||
| 我们还会遇到这种问题,当发布 JavaScript 代码到生产环境时,我们会使用 *minifier* 压缩代码 —— 这是一个特别的程序,它会移除代码中多余的注释、空格等以减小文件 —— 更重要的是,它会用更短的字符重命名所有的本地变量。 |
| 但是在这种情况下,如果使 `new Function` 可以访问自身函数以外的变量,它也很有可能无法找到 `userName`,这是因为新函数的创建发生在代码压缩以后,变量名已经被替换了。 | ||
|
|
||
| **Even if we could access outer lexical environment in `new Function`, we would have problems with minifiers.** | ||
| **即使我们可以在 `new Function` 中访问外部词法空间,我们也会受挫于 minifier。** |
| `new Function` 的这种特性看起来有点奇怪,不过在实战中却非常实用。 | ||
|
|
||
| Imagine that we must create a function from a string. The code of that function is not known at the time of writing the script (that's why we don't use regular functions), but will be known in the process of execution. We may receive it from the server or from another source. | ||
| 想象我们必须用字符串来创建一个函数。在编写脚本时我们不会知道新函数的代码(这也是为什么我们不用常规方法创建函数),我们只能在运行时从服务器或其他源获取代码的内容。 |
| } | ||
|
|
||
| getFunc()(); // *!*"test"*/!*, from the Lexical Environment of getFunc | ||
| getFunc()(); // *!*"test"*/!*,变量值取自 getFunc 的词法空间 |
Member
|
@lcx-seima 可以修改了,一个校对够了。 |
Contributor
Author
|
@Starriers 感谢校对 |
leviding
approved these changes
May 8, 2018
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
完成翻译 resolve #74