将语句转换为 ES5

Convert a statement to ES5

我需要帮助将下面的语句转换为 ES5 语法。在 ES5 中会是什么?

const { a, b, c = “foo” } = this.props;
 var
   a=this.props.a,
   b=this.props.b,
   c=this.props.c||"foo";

对象解构试图在对象中找到同名的属性,所以

 {prop}=obj

等于:

 prop=obj.prop;

可以使用 Or 运算符轻松实现默认参数:

 prop=obj.prop || default;

或者如果你想将 falsys 算作道具,它将是:

 prop=("prop" in obj)?obj["prop"]:default;

我相信会是:

var a = this.props.a,
    b = this.props.b,
    c = this.props.c || "foo"

至少我希望如此,否则投票会下雨

实现此目的的最简单方法是:

var a = this.props.a,
b = this.props.b,
c = this.props.c===undefined?"foo":this.props.c

但更好的方法是只使用 this.props 对象而不是将其存储在局部变量中。

我建议使用显式检查 属性 c 是否存在于给定对象中。如果没有给出,则使用默认值。

var a = this.props.a,
    b = this.props.b,
    c = this.props.c === undefined ? 'foo' : this.props.c;

另外使用的模式

c = this.props.c || 'foo';

不适用于给定的虚假值,如零。

Why do you need a check with undefined (kudos to loganfsmyth for mention this problem in comments)?

Because undefined is the value for the check for default parameters in a function in ES6.

const f = (c = 'foo') => console.log(c);

f();          // 'foo'
f(undefined); // 'foo'
f(0)          // 0