Icontem

ES5 Spread Operator: Emulate the ES6 spread operator under EcmaScript 5

Recommend this page to a friend!
  Info   View files Documentation   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2015-06-26 (1 year ago) RSS 2.0 feedNot yet rated by the usersTotal: 46 All time: 483 This week: 44Up
Version License JavaScript version Categories
es5spread 1.0Freely Distributable1.0Data types, Language
Description Author

This package can emulate the ES6 spread operator under EcmaScript 5.

It adds a new function prototype named __ that works like the spread operator by expanding array function parameters into the values contained in the array so act as if the calling code passed the array entries as parameter values to the function.

Additionally this package provides functions that return iterator objects to get entries, keys and values of an array.

Innovation Award
JavaScript Programming Innovation award nominee
June 2015
Number 3
EcmaScript 6 is the basis of the next JavaScript version. However most current browsers use a JavaScript implementation based on EcmaScript 5, so they do not benefit of the new language features.

One of those features is the spread operator. It allows expanding arrays to be passed to functions that take variable lists of arguments.

This package provides an alternative that works with EcmaScript 5 based browsers to emulate the spread operator of EcmaScript 6.

Manuel Lemos
  Performance   Level  
Name: Stephen Chapman <contact>
Classes: 11 packages by
Country: Australia Australia
Age: ???
All time rank: 191 in Australia Australia
Week rank: 8 Up1 in Australia Australia Equal
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Details
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.
  Files folder image Files  
File Role Description
Accessible without login Plain text file description Doc. How to use the ES5 spread operator
Plain text file spread.js Class A spread operator for ES5

 Version Control Unique User Downloads Download Rankings  
 0%
Total:46
This week:0
All time:483
This week:44Up