-
Notifications
You must be signed in to change notification settings - Fork 5k
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
笔记 6. ES6:参数默认值 #5703
笔记 6. ES6:参数默认值 #5703
Conversation
笔记 6. ES6:参数默认值
校对认领 |
@fireairforce 好的呢 🍺 |
校对认领 |
@xingqiwu55555 妥妥哒 🍻 |
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.
翻译得很好,文章对我本身也很有帮助,谢谢。
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.
翻译还是棒棒的,就是很多地方其实可以语义化解释,让人更易理解。
|
||
In this small note we’ll cover another ES6 feature, function parameters with _default values_. As we will see there are some subtle cases there. | ||
在这篇文章中我们会涉及另一个 ES6 的特性,带**默认值**的函数参数。正如我们将看到的,有一些微妙的案例。 |
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.
『涉及』=>『介绍』
default values 不知道这里符号是否是加粗样式** **
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.
采纳。是加粗的。
@@ -23,27 +23,27 @@ log('low memory'); // warning: low memory | |||
log('out of memory', 'error'); // error: out of memory | |||
``` | |||
|
|||
To avoid possible “falsey values”, often one can see `typeof` check: | |||
为了避免潜在的“假值”,通常可以看到有 `typeof` 检查: |
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.
这里可以语义化一点,『为了避免参数未传递的情况』
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.
这里可以语义化一点,『为了避免参数未传递的情况』
采纳。
|
||
Below are several specific to ES6 implementation details of function default parameter values. | ||
以下是关于 ES6 函数默认参数值的实现细节的一些特性。 |
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.
这里的 several specific 不是『一些特性』的意思,可以不用译出来:
『以下是一些关于 ES6 函数默认参数值的实现细节。』
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.
采纳。
# just a property of the function | ||
# 我们可以看到默认值只被创建一次,即 | ||
# 在定义函数时,并且仅作为 | ||
# 函数的一个属性被保存起来 |
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.
这里其实只有两句,在函数定义时,既是上一句的状从也是下一句的状从,所以这里可以把即去掉:
『# 我们可以看到默认值在函数定义时只创建了一次』
『# 并且保存于函数对象的属性中』
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.
这里其实只有两句,在函数定义时,既是上一句的状从也是下一句的状从,所以这里可以把即去掉:
『# 我们可以看到默认值在函数定义时只创建了一次』
『# 并且保存于函数对象的属性中』
『# 我们可以看到默认值在函数定义时只创建了一次』:这是最初的译法,但是考虑到行号要对应以方便查看翻译前后的差异,所以进行了拆分。采纳。
『# 并且保存于函数对象的属性中』:原文提到了属性,所以这里突出“函数是对象”这一点确实更好。采纳。
@@ -100,7 +100,7 @@ foo() # [1] | |||
print(foo.__defaults__) # ([None],) | |||
``` | |||
|
|||
However, this is the same manual inconvenient handling of the actual default value, and the initial case is just confusing. That’s said, to avoid this, ECMAScript defaults are evaluated on every function execution: | |||
但是,这同样是不方便的手动处理实际默认值的方式,并且最初的案例让人感到疑惑。因此,为了避免这种情况,ECMAScript 会在每次函数执行时计算默认值: |
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.
『这同样是不方便的手动处理实际默认值的方式』=>『这与手动处理实际默认值的方式是一样不方便的』
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.
『这同样是不方便的手动处理实际默认值的方式』=>『这与手动处理实际默认值的方式是一样不方便的』
采纳。
@@ -113,11 +113,11 @@ foo(); // [1] | |||
foo(); // [1] | |||
``` | |||
|
|||
All good and intuitive. Now let’s see when ES semantics may also confuse if not to know how it works. | |||
一切都很好,很直观。现在我们来看下不了解工作机制时,ES 语义何时会让我们感到困惑。 |
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.
这句话总觉得不太通顺:
『现在我们来看下不了解工作机制时,ES 语义何时会让我们感到困惑。』=>『接下来你会发现,如果我们不了解默认值的工作机制,ES 语义可能会让我们感到困惑。』
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.
这句话总觉得不太通顺:
『现在我们来看下不了解工作机制时,ES 语义何时会让我们感到困惑。』=>『接下来你会发现,如果我们不了解默认值的工作机制,ES 语义可能会让我们感到困惑。』
这句话翻译的时候确实拿不准,适当意译会好一点。采纳。
... | ||
} | ||
``` | ||
|
||
The assignment `= x` as we mentioned above resolves `x` in the parameters scope, that shadowed the global `x`. However, the parameter `x` is under the TDZ, and cannot be accessed until it’s initialized. Obviously, it cannot be initialized to itself. | ||
我们上面提到的赋值 `= x` 在参数作用域中解析 `x` ,遮蔽了全局 `x` 。 但是,参数 `x` 位于 TDZ 内,在初始化之前无法访问。显然,它无法初始化为自身。 |
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.
显然 可以 译为 因此,毕竟是呈上 参数不能以自身作为默认值。
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.
显然 可以 译为 因此,毕竟是呈上 参数不能以自身作为默认值。
采纳。
|
||
``` | ||
var x = 1; | ||
|
||
function foo(x, y = function() { x = 2; }) { | ||
var x = 3; | ||
y(); // is `x` shared? | ||
console.log(x); // no, still 3, not 2 | ||
y(); // `x` 被共用了吗? |
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.
『x
被共用了吗?』=>『局部变量 x
被改写了吗?』
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.
『
x
被共用了吗?』=>『局部变量x
被改写了吗?』
如果 x
被共用,则会被改写,作者这里是通过说明“ x
没有被两个作用域共用”,进而说明” x
没有被改写“这件事。因此,我认为保留原来的译文比较好。你觉得呢?
``` | ||
|
||
Now we see that when function `y` is executed, it resolves `x` in the nearest environment (i.e. in the same environment), and doesn’t even see the scope of the function. | ||
我们可以看到,当函数 `y` 执行时,它在最近的环境(即相同的环境)中解析 `x` ,甚至无视了函数的作用域。 |
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.
『即相同的环境』=>『即参数环境』
『甚至无视了函数的作用域』=>『函数作用域对其并不可见』
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.
『即相同的环境』=>『即参数环境』
『甚至无视了函数的作用域』=>『函数作用域对其并不可见』
采纳。有个小小的建议哈,同一个句子如果有一个以上的错误的话,最好再开一个新的conversation,这样可以针对单独的修改作出回复。
``` | ||
|
||
#### When the params scope is not created | ||
#### 何时不会创建参数作用域 |
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.
『何时不会创建参数作用域』=>『当不创建参数作用域时』
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.
『何时不会创建参数作用域』=>『当不创建参数作用域时』
『当不创建参数作用域时』:这个是最初的译法,但是考虑到下文作者说的是什么时候不会创建参数作用域(参数没有设置默认值时)以及为什么不统一创建参数作用域(向下兼容),所以改为『何时不会创建参数作用域』。这里我觉得保留原来的译文比较好,你觉得呢?
@fireairforce @xingqiwu55555 感谢校对,辛苦了。 |
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.
@Chorer 细节问题注意一下
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
Co-Authored-By: Chorer <43442630+Chorer@users.noreply.github.com>
@leviding 已修改。以后会多加注意:1.全角符号和半角符号 2.空格 |
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.
文章这个标题不是很吸引人,可以改个标题
@Chorer 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。 掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件。 |
《ES6:理解参数默认值的实现细节》。这个标题可以吗?感觉用“深入理解”不太合适,因为文章其实也没有特别深入地讲。 |
译文翻译完成,resolve #5702