File: description

Recommend this page to a friend!
  Classes of Stephen Chapman  >  ES5 Spread Operator  >  description  >  Download  
File: description
Role: Documentation
Content type: text/plain
Description: How to use the ES5 spread operator
Class: ES5 Spread Operator
Emulate the ES6 spread operator under EcmaScript 5
Author: By
Last change: New package
Date: 5 years ago
Size: 1,081 bytes
 

Contents

Class file image Download
The latest version of JavaScript allows you to pass variables, arrays and iterators into a function using code such as:

var a = 1;
var b = [2,3];
var c = [4,5,6].values();
fn(a, ...b, ...c);

The ES2015 spread operator (eg ...c) is not recognised in older versions of JavaScript. While we can't get old versions of JavaScript to recognise this new syntax, we can implement a similar syntax to achieve the same end result.

By implementing this spread method we can make a milor change to the above code and have it run in any version of JavaScript.

var a = 1;
var b = [2,3];
var c = [4,5,6].values();
fn.__(a, b, c)



Bonus
-----

Generate iterators from arrays with these polyfills (not found in any of the usual places you'd look for polyfills)
Array.entries( ) - returns an array Iterator object containing key/value pairs for each entry in the array.

Array.keys() - returns an array Iterator object containing the key for each entry in the array.

Array.values() - returns an array Iterator object containing the value for each entry in the array.