JS default argument value from variable: why must identifier be different? [duplicate]

好久不见. 提交于 2019-12-01 04:47:19

The purpose of this behavior is to allow a parameter to be default-initialized to the value of another parameter, e.g:

var a = 2;
var x = (a, b = a) => console.log(a, b);
x(42); // 42 42

Making the special case a = a work differently could be done but that would make it harder to refactor functions that use this behavior (you wouldn't be able to rename the parameter a without also renaming the variable that it depends on).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!