我可以通过获取向量的切片和对元组中项目的引用来解构元组向量:
let items = vec![("Peter".to_string(), 180)];
if let [(ref name, ref age)] = items.as_slice() {
println!("{} scored {}", name, age);
};
Run Code Online (Sandbox Code Playgroud)
如何直接解构向量,将项目移出元组。像这样的东西:
let items = vec![("Peter".to_string(), 180)];
if let [(name, age)] = items {
println!("{} scored {}", name, age);
};
Run Code Online (Sandbox Code Playgroud)
编译上述结果导致错误:
let items = vec![("Peter".to_string(), 180)];
if let [(ref name, ref age)] = items.as_slice() {
println!("{} scored {}", name, age);
};
Run Code Online (Sandbox Code Playgroud) 我正在使用解构来声明一些像这样的变量:
const { a, b, c } = require('./something'),
{ e = 'default', f = 'default'} = c;
Run Code Online (Sandbox Code Playgroud)
有没有办法把它变成单行?我尝试过类似的东西:
const { a, b, c = { e = 'default', f = 'default'} } = require('./something');
Run Code Online (Sandbox Code Playgroud)
但它给了我一个错误:
SyntaxError:无效的简写属性初始值设定项
有没有办法实现方法参数解构,但也能得到方法参数.
在具有无状态组件的React应用程序的上下文中,我希望能够替换
const MyComponent = (props) => {
const {prop1, prop2} = props;
return (
<div className={prop1 + '-' + prop2}>
<Child {...props}/>
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
用更简洁的语法
const MyComponent = (props: {prop1, prop2}) (
<div className={prop1 + '-' + prop2}>
<Child {...props}/>
</div>
)
Run Code Online (Sandbox Code Playgroud)
有没有这样的语法?
我试图弄清楚是否有可能通过解构处理多个级别的默认参数.由于用文字解释起来并不容易,这是一个循序渐进的例子......
解构这个对象很简单:
let obj = {
foo: 'Foo',
bar: 'Bar'
};
Run Code Online (Sandbox Code Playgroud)
随着{foo = 'Foo', bar = 'Bar'} = {}在函数签名,如果没有当函数被调用时传入的参数的对象将被创建.如果传递了一个对象但是引用了一些属性undefined,则它们将被默认值替换.这段代码工作正常:
function fn1({foo = 'Foo', bar = 'Bar'} = {}) {
console.log(foo, bar);
}
// OK
fn1(); // Foo Bar
fn1({foo: 'Quux'}); // Quux Bar
fn1({bar: 'Quux'}); // Foo Quux
fn1({foo: 'Quux', bar: 'Quux'}); // Quux QuuxRun Code Online (Sandbox Code Playgroud)
解构这个对象更难:
let obj = {
foo: 'Foo',
bar: {
quux: 'Quux',
corge: 'Corge'
}
}; …Run Code Online (Sandbox Code Playgroud) javascript function destructuring default-parameters ecmascript-6
让我说我有这个代码:
const {x, y} = point;
Run Code Online (Sandbox Code Playgroud)
巴别塔会把它变成:
var _point = point,
x = _point.x,
y = _point.y;
Run Code Online (Sandbox Code Playgroud)
这很好,但如果点未定义怎么办?现在我收到一个错误:
"Cannot read property 'x' of undefined".
那么我该如何避免这种情况呢?
我想做点什么
const {x, y} = {} = point;
Run Code Online (Sandbox Code Playgroud)
但这是一个语法错误.
我只能看到这是一个选项:
const {x, y} = point || {};
Run Code Online (Sandbox Code Playgroud)
哪个巴贝尔转向:
var _ref = point || {},
x = _ref.x,
y = _ref.y;
Run Code Online (Sandbox Code Playgroud)
这里我们创建一个对象只是为了避免未定义的错误.这似乎很浪费.
是否有一些我想要的语法会避免这种情况?可能会发生类似这样的事情:
var x, y;
if (typeof point !== 'undefined') {
x = point.x;
y = point.y;
}
Run Code Online (Sandbox Code Playgroud) 我有一个结构:
struct ThreeDPoint {
x: f32,
y: f32,
z: f32
}
Run Code Online (Sandbox Code Playgroud)
我想在实例化之后提取三个属性中的两个:
let point: ThreeDPoint = ThreeDPoint { x: 0.3, y: 0.4, z: 0.5 };
let ThreeDPoint { x: my_x, y: my_y } = point;
Run Code Online (Sandbox Code Playgroud)
编译器抛出以下错误:
error[E0027]: pattern does not mention field `z`
--> src/structures.rs:44:9
|
44 | let ThreeDPoint { x: my_x, y: my_y } = point;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing field `z`
Run Code Online (Sandbox Code Playgroud)
在JavaScript(ES6)中,等效的解构将如下所示:
let { x: my_x, y: my_y } = point;
Run Code Online (Sandbox Code Playgroud) 我有两个javascript对象与下一个语法:
var section = { name: "foo", tables: [] }
var field = {name: "bar", properties: {} }
Run Code Online (Sandbox Code Playgroud)
和一个期望这些对象的函数,但在函数中我只使用每个对象的名称,所以我想知道我是否可以在函数声明中解构两个对象,如:
function something( {name}, {name} ) {
//code
}
Run Code Online (Sandbox Code Playgroud)
第一个应该是section.name,第二个应该是field.name.
在这种情况下,有两种方法可以进行解构吗?或者我应该只观看功能中的名字?
哪个更好?
谢谢.
我正在学习javascript,并且在尝试在解构时为变量赋予默认值时,我有点坚持使用ES6语法.基本上,我试图分配一个给出对象属性值的变量,如果值为false/null/undefined,我希望它是一个空对象.例如,
let foo = {
prop1: 'hello!',
prop2: null
}
const prop1 = foo.prop1 || {}
const prop2 = foo.prop2 || {}
console.log(prop1) // hello!
console.log(prop2) // {}
Run Code Online (Sandbox Code Playgroud)
这就是我想要的,并且我认为ES6糖语法相当于上面(它不起作用......)
let foo = {
prop1: 'hello!',
prop2: null
}
const { prop1 = {} } = foo
const { prop2 = {} } = foo
console.log(prop1) // hello!
console.log(prop2) // null
Run Code Online (Sandbox Code Playgroud)
但不知何故,有时它似乎在React中工作,但其他时候它没有...它是兼容性问题吗?太混乱了!
javascript destructuring variable-assignment ecmascript-6 reactjs
这里有一些解构:
const { [a]: b } = this.props
Run Code Online (Sandbox Code Playgroud)
但是,做了[a]: b什么:带冒号的括号是做什么的?在我的情况下,a作为一个带有字符串值的道具提供.
我今天正在尝试一些事情,并且遇到了我想要理解的行为.
var b = ({a = 1, b = 1, c = 1}) => a + b + c;
b(); // throws error.
Run Code Online (Sandbox Code Playgroud)
但如果它是这样定义的
var b = ({a = 1, b = 1, c = 1} = 0) => a + b + c;
b() // returns 3
b([]) // returns 3
Run Code Online (Sandbox Code Playgroud)
这不应该是一个错误吗?零在某种程度上成为了一个对象吗?它在某种程度上等同于以下?
var b = ({a = 1, b = 1, c = 1} = {}) => a + b + c; // this is possible I guess.
Run Code Online (Sandbox Code Playgroud)
我的问题不是常规的destrcuturing和default …
destructuring ×10
javascript ×8
ecmascript-6 ×6
reactjs ×3
rust ×2
babel ×1
declaration ×1
function ×1
slice ×1
vector ×1