如果var前面没有关键字,为什么对象解构会抛出错误?
{a, b} = {a: 1, b: 2};
Run Code Online (Sandbox Code Playgroud)
投 SyntaxError: expected expression, got '='
以下三个示例没有问题
var {a, b} = {a: 1, b: 2};
var [c, d] = [1, 2];
[e, f] = [1, 2];
Run Code Online (Sandbox Code Playgroud)
奖金问题:为什么我们不需要var进行阵列解构?
我遇到了类似的问题
function () {
var {a, b} = objectReturningFunction();
// Now a and b are local variables in the function, right?
// So why can't I assign values to them?
{a, b} = objectReturningFunction();
}
Run Code Online (Sandbox Code Playgroud) 我今天遇到了一个问题,请考虑以下组件:
export default class Input extends React.Component {
someFunction() {
console.log(this.props.value)
}
render () {
const { type, value, required } = this.props
return (
<div className={cx('Input')}>
<input type={type} value={value} required={required} />
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
我成功地破坏了,this.props并且可以在渲染中使用它们,但是如果我需要在它之外使用道具值,即内部someFunction()我不确定如果我移出constant { ... }并包括export default class Input extends React.Component {一行一行会产生什么后果.这仍然有效吗?
以下代码有效.有没有一种更方便的方式,如果可能的话甚至是一个单行程?
const { nextUrl, posts } = await postService.getCommunityPosts(6);
this.communityPosts = posts;
this.nextUrl = nextUrl;
Run Code Online (Sandbox Code Playgroud)
我知道给出了结构化属性别名,但我不认为在这种情况下有帮助.MDN对此案没有任何说明.