我正在清理一些sequelize代码,findOrCreate函数返回一个需要传播以获取实际结果对象的promise.
我想改写我的代码来await代替使用,并且,鉴于ES6支持数组解构,我认为不是
User.findOrCreate({ where: { mcId }, defaults }).spread((user, created) => {
// do stuff
})
Run Code Online (Sandbox Code Playgroud)
我能做到的
const [user, created] = await User.findOrCreate({ where: { mcId }, defaults })
Run Code Online (Sandbox Code Playgroud)
但唉,事实并非如此.
我收到了错误 (intermediate value) is not iterable
这样做有什么特别的伎俩,或者我想做的事情是不可能的?
我正在尝试理解Dan Abramov发布的Redux在线教程.目前我在以下样本:
以下是我在上面的示例后的练习代码:
// Individual TODO Reducer
const todoReducer = (state, action) => {
switch(action.type) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
completed: false
};
case 'TOGGLE_TODO':
if (state.id != action.id) return state;
// This not working
/*
return {
...state,
completed: !state.completed
};
*/
//This works
var newState = {id: state.id, text: state.text, completed: !state.completed};
return newState;
default:
return state;
}
};
//TODOS Reducer
const todos = (state = [], action) => {
switch(action.type) {
case …Run Code Online (Sandbox Code Playgroud) 但是使用带有Arguments对象的扩展语法有任何已知的缺点(优化/性能问题)吗?或者这完全可以吗?
我想从传递给函数的未知数量的参数创建一个数组:
function Numbers(){
this.numbers = [...arguments];
}
Run Code Online (Sandbox Code Playgroud)
它看起来很整洁,在MDN页面中有关Arguments对象甚至建议我可以使用spread运算符:
您还可以使用
Array.from()方法或扩展运算符将参数转换为实数数组.
但我还是想看看其他人是否对此有另一种看法.
如果o对象包含一对键/值对:foo: 'bar'我可以依赖这些结果吗?:
// foo will be 'bar'
<MyComponent
foo='should not override'
{...o}
/>
// foo will be 'overridden'
<MyComponent
{...o}
foo='overridden'
/>
Run Code Online (Sandbox Code Playgroud)
换句话说,使用扩展运算符时属性的排序是否显着?
我第一次遇到 JavaScript 中的 spread( ...) 语法,并且逐渐欣赏它可以做的许多事情,但我承认我仍然觉得它很奇怪。其他语言中是否有等效项?在那里它叫什么?
在JavaScript中,可以使用扩展语法将对象扩展到另一个对象:
const a = {one: 1, two: 2}
const b = {...a, three: 3} // = {one: 1, two: 2, three: 3}
Run Code Online (Sandbox Code Playgroud)
有没有办法以这种方式将打字稿界面传播到另一个界面?
interface IA {
one: number;
two: number;
}
interface IB {
...IA; // Does not work like this
three: number;
}
Run Code Online (Sandbox Code Playgroud)
这样生成的界面IB看起来像这样:
{
one: number;
two: number;
three: number;
}
Run Code Online (Sandbox Code Playgroud) 我正在构建一个 React 应用程序,我正在导入一个使用扩展运算符的节点模块,但...在此运算符处出现错误“模块解析失败:意外令牌 (100:6)”。我怎样才能让它发挥作用?
谢谢
基本上这就是我想要完成的事情.
class Person {
constructor (obj) {
this.first = ''
this.last = ''
this.age = ''
if (obj) {
Object.assign(this, ...obj)
}
}
}
const a = new Person()
console.log('Not spreading: ', a)
const b = new Person({ first: 'Alex', last: 'Cory', age: 27 })
console.log('Spreading: ', b)Run Code Online (Sandbox Code Playgroud)
有没有办法传播这样的对象来填充一个类?
我正在尝试使用扩展运算符更新对象的嵌套值。这是我第一次使用它,我相信我已经接近实现我的最终目标了,但我似乎无法弄清楚接下来我真正需要做什么。
我有一个结构如下的数组:
[
{
name: "Category 1",
posts: [
{
id: 1,
published: false,
category: "Category 1"
},
{
id: 2,
published: true,
category: "Category 1"
}
]
},
{
name: "Category 2",
posts: [
{
id: 3,
published: true,
category: "Category 2"
},
{
id: 4,
published: true,
category: "Category 2"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
单击按钮时,我尝试更新已发布的值,当我使用 React 时,我需要设置状态。所以有人建议我使用扩展运算符进行更新。
onPostClick(post) {
post.pubished = !post.published;
this.setState({...this.state.posts[post.category], post})
}
Run Code Online (Sandbox Code Playgroud)
如果我注销结果,{...this.state.posts[post.category], post}我可以看到已发布的内容正在添加到形成的父级中:
{
name: "Category 1",
published: false,
posts: [
... …Run Code Online (Sandbox Code Playgroud) 我注意到 Object Spread 语法对于它可以接受的值类型非常宽松:
console.log({ ...true });
console.log({ ...false });
console.log({ ...0 });
console.log({ ...42 });
console.log({ ...-1 });
console.log({ ...NaN });
console.log({ ...'batman' });
console.log({ .../\w+[0-9a-fA-F]?/ });
console.log({ ...['foo', 'bar', 42] });
console.log({ ...undefined });
console.log({ ...false });
console.log({ ...Symbol('hmm') });
console.log({ ...Promise.resolve('resolved') });
console.log({ ...Promise.reject('rejected') });Run Code Online (Sandbox Code Playgroud)
在对象字面量中传播时,是否存在无效的类型、类或值(即引发任何类型的错误)?当然,不包括未捕获的拒绝承诺。
spread-syntax ×10
javascript ×8
ecmascript-6 ×4
reactjs ×3
arguments ×1
arrays ×1
async-await ×1
class ×1
interface ×1
object ×1
polyfills ×1
promise ×1
react-jsx ×1
redux ×1
typescript ×1