javascript JavaScript Spread运算符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript JavaScript Spread运算符相关的知识,希望对你有一定的参考价值。

var someMore = [10,12,14,16,18];
/* ES5 */
var all = [2,4,6,8 ].concat(someMore); 
// Output : [ 2, 4, 6, 8, 10, 12, 14, 16, 18 ]

/*  ES6 - Spread Operator  */
var evens = [2,4,6,8, ...someMore];
console.log(evens);
// Output : [ 2, 4, 6, 8, 10, 12, 14, 16, 18 ]


var myName = "Ozesh";
/*  ES5 */
var chars = myName.split("");
console.log(chars);
// Output :  [ "O", "z", "e", "s", "h" ]

/*  ES6 - Spread Operator */
var chars =[...myName];
console.log(chars);
// Output : [ "O", "z", "e", "s", "h" ]


/* Object Spread */
let data = { name: 'Ojesh', address: 'Ktm'
};
console.log(data);
// Output : { name: 'Ojesh', address: 'Ktm' }

// With Object Spread
let info = {...data,  age: 25 }
console.log(options);
// Output : { name: 'Ojesh', address: 'Ktm', age: 25 }

// Without Object Spread
let info = Object.assign({}, defaults, { age: 25 });


/* Array Spread */
var nums1 = [1,2,3];
var nums2 = [4,5,6];

// With Array Spread
const allNums = [...nums1,...nums2, 7 ];
console.log(allNums);

// Without Array Spread
const allNums = nums1.concat(nums2).concat([ 7 ])

// Output : [ 1, 2, 3, 4, 5, 6, 7 ]

以上是关于javascript JavaScript Spread运算符的主要内容,如果未能解决你的问题,请参考以下文章

Pygame错误:self.spritedict [spr] = surface_blit(spr.image,spr.rect)

SpringBoot rest-api+Vue CORS跨域.md

五大JAVA Web框架的优缺点对比,Spring MVC领先

使用Spire组件抛出异常The type initializer for 'spr857' threw an exception

我的大学,我的SPR

JavaScript介绍