微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

对象字面量 ({...object}) 中的传播语法有何用途?

如何解决对象字面量 ({...object}) 中的传播语法有何用途?

我已经阅读了关于 I don't understand about spread syntax inside objects 的答案,但仍然不太明白使用(特别是)class CarouselWithindicatorDemo extends StatefulWidget { @override State<StatefulWidget> createState() { return _CarouselWithindicatorState(); } } class _CarouselWithindicatorState extends State<CarouselWithindicatorDemo> { int _current = 0; List<String> imgList = [ 'https://images.unsplash.com/photo-1520342868574-5fa3804e551c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=6ff92caffcdd63681a35134a6770ed3b&auto=format&fit=crop&w=1951&q=80','https://images.unsplash.com/photo-1522205408450-add114ad53fe?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=368f45b0888aeb0b7b08e3a1084d3ede&auto=format&fit=crop&w=1950&q=80','https://images.unsplash.com/photo-1519125323398-675f0ddb6308?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=94a1e718d89ca60a6337a6008341ca50&auto=format&fit=crop&w=1950&q=80','https://images.unsplash.com/photo-1523205771623-e0faa4d2813d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=89719a0d55dd05e2deae4120227e6efc&auto=format&fit=crop&w=1953&q=80','https://images.unsplash.com/photo-1508704019882-f9cf40e475b4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8c6e5e3aba713b17aa1fe71ab4f0ae5b&auto=format&fit=crop&w=1352&q=80','https://images.unsplash.com/photo-1519985176271-adb1088fa94c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a0c8d632e977f94e5d312d9893258f59&auto=format&fit=crop&w=1355&q=80' ];//Use any photos u want @override Widget build(BuildContext context) { return Container( width:MediaQuery.of(context).size.width,//Fills the page horizontally child:Stack( children: [ CarouselSlider( items: imageSliders,options: CarouselOptions( autoplay: true,enlargeCenterPage: true,aspectRatio: 2.0,onPageChanged: (index,reason) { setState(() { _current = index; }); } ),),Column( mainAxisAlignment:MainAxisAlignment.end children:<Widget>[ Row( mainAxisAlignment: MainAxisAlignment.center,children: imgList.map((url) { int index = imgList.indexOf(url); return Container( width: 14.0,height: 14.0,margin: EdgeInsets.symmetric(vertical: 10.0,horizontal: 2.0),decoration: Boxdecoration( shape: BoxShape.circle,color: _current == index ? Colors.white,: Color.grey[400],); }).toList(),SizedBox(height:30),],] ); } } 的目的。

{...object} 的用途是什么?

我在节点 REPL 中对此进行了测试,假设我创建了一个对象:

{...object}

并在新的对象字面量中使用扩展运算符来引用它:

> const object = { foo: "hello",bar: "world" };

输出与仅使用对象本身相同:

> { ...object }
{ foo: 'hello',bar: 'world' }

> object { foo: 'hello',bar: 'world' } 的用途是什么?

解决方法

这不是同一个对象。它制作了对象自己的可枚举属性的浅拷贝(如 Object.assign({},object) (MDN)。您可以在需要时使用它......一个副本,具有对象自己的可枚举属性。:-D

例如,对于多个 MVC 或类似库中的任何一个,您可以在更新状态时使用它,因为不应直接修改状态:

this.setState(oldState => ({...oldState,prop: "new value"}));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?