1.使用语法糖(三点运算符)
<script type="text/babel"> class MyComponet extends React.Component{ render(){ const {name,age,address}=this.props; return ( <ul> <li>name is{name}</li> <li>age is{age}</li> <li>address is {address}</li> </ul> ) } } const p={name:"laozhichi",age:19,address:"leizhou"} ReactDOM.render(<MyComponet {...p}/>,document.getElementById("test")); </script>
2.三点运算符实例
let arr1=[1,2,3,4]; let arr2=[5,6,7,8]; console.log(...arr1);//在这里将数组分开表示,不要以括号的形式表示 console.log(arr1); console.log(...arr1,...arr2);// 连接数组 function add(...numbers){ //在这里表示将所有传入的数字全部相加 return numbers.reduce((preValue,currentValue)=>{ return preValue+endValue; }) } console.log(add(12,56,89,56,989,98,66,56));
3.拷贝
let person={name:"laozhichi",age:19}; let person1=person; person.name="laozhichao" console.log(person1.name);
4.拷贝对象
let person={name:"laozhichi",age:19}; let person1={...person}; console.log(person1);
5.合并对象
let person={name:"laozhichi",age:19}; let person1={...person,name:"laozhichao",address:"leizhou"}; //在合并的时候,laozhichao会覆盖laozhichi console.log(person1);