The Spread Operator …

Modern features of JavaScript

The spread operator, which is denoted by three dots  , is a very versatile and powerful feature of modern JavaScript.

It has four major uses:

  1. Function application
  2. Working with arrays*
  3. Working with objects

* array spreads are just a special case of spreads with Iterables (more soon).

Function application

Function application simply means calling functions but JavaScript functions come with an apply() method that enables you to call that function while passing an array for arguments.

f = (a, b, c) => a + b + c;
args = [ 1, 2, 3 ]
f.apply(null, args);

Result

> 6

You can use the spread operator as an alternative to apply().

f(...args);

You can also mix and match your arguments

f(10, ...[20, 30])
f(...[10, 20], 30)
f(10, ...[20], 30)

Result

> 60

Working with Arrays

For a given array:

arr1 = [ 1, 2, 3 ];

you can use the spread operator to create a shallow copy of the array

arr2 = [ ...arr2 ];

Result

> arr2 = [ 1, 2, 3 ];

You can also use the spread operator to prepend an element to an array

arr1 = [ 1, 2, 3 ];
arr2 = [ 0, ...arr1 ];

Result

> arr2 = [ 0, 1, 2, 3 ]

Or you can use the spread operator to append an element to an array

arr1 = [ 1, 2, 3 ];
arr2 = [ ...arr1, 4 ];

Result

> arr2 = [ 1, 2, 3, 4 ]

you can also use the spread operator to concatenate multiple arrays

arr1 = [ 1, 2, 3 ];
arr2 = [ 4, 5, 6 ];
arr3 = [ 7, 8, 9 ];
arr4 = [ ...arr1, ...arr2, ...arr3 ];

Result

> arr4 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

The spread operator can be used in any location of the receiving object and be mixed with normal key assignments

arr1 = [ 1, 2, 3 ];
arr2 = [ 4, 5, 6 ];
arr3 = [ 100, ...arr1, 200, ...arr2, 300 ];

Result

> arr3 = [ 100, 1, 2, 3, 200, 4, 5, 6, 300 ]

Working with Objects

The spread operator for objects works in a very similar way to the spread operator for arrays/iterables but its behaviour is subtly different. It is useful for spreading the key-value pairs of an object into another object

For a given object:

obj1 = { a: 1, b: 2, c: 3 };

you can create a shallow copy using the spread operator

obj2 = { ...obj1 };

Result

> obj2 = { a:1, b:2, c:3 }

you can also use the spread operator to merge multiple objects together

obj1 = { a: 1, b: 2, c: 3 };
obj2 = { d: 4, e: 5, f: 6 };
obj3 = { g: 7, h: 8, i: 9 };
obj4 = { ...obj1, ...ob2, ...obj3 };

Result

> obj4 = { a:1, b:2, c:3, d:4, e:5, f:6, g: 7, h: 8, i: 9 }

you can use the spread operator in any location of the receiving object and mix it with normal key-value assignments

obj1 = { a: 1, b: 2, c: 3 };
obj2 = { d: 4, e: 5, f: 6 };
a = 100
b = 200
obj3 = { a, ...obj1, b, ...ob2, c: 300 };

Result

> obj3 = { a: 1, b: 200, c: 300, d: 4, e: 5, f: 6 }

NOTE: If keys get repeated the value gets overridden with the last assigned value.

NOTE: Key order is preserved in JavaScript based on insertion order.

obj1 = { a: 1, b: 2, c: 3 };
obj2 = { c: 10, ...obj1 }

Result

> obj2 = { c: 3, a: 1, b: 2 }
Object.keys(obj2);
> [ 'c', 'a', 'b' ]

Object Spreads with other object types

Because the spread operator for objects treats its argument as a simple object. This means that it treats arrays and other iterable types as objects which have their index as a key.

Given the array

arr1 = [ 10, 20, 30 ]

The object spread operator will treat the array as simple object

obj1 = { ...arr1 };

Result

> obj1 = { 0: 10, 1: 20, 2: 30 }

Given the string

str1 = 'Hello';

The object spread operator will treat the string as simple object

obj1 = { ...str1 };

Result

> obj1 = { 0: 'H', 1: 'e', 2: 'l', 3: 'l', 4: 'o' }

Installing Win 10 on a Macbook for FREE

Here are my notes for turning a Macbook Pro into a very nice Windows 10 machine for free using Bootcamp. This is not for everybody, I know, so why would I even do such a thing?

1. I find OSX hard to switch to when I am forced to use Windows all day on another work issued laptop. So this Macbook is hardly used.

2. Actually OSX is terrible. Objectively I like Windows 10 better.

3. Operating systems should just get the fuck out of my way. I’d be just as happy using Ubuntu but Windows is pretty good. Git Bash gives me all the Linux compatibility I need.

4. I do web development. That means I don’t need many binaries. Everything I need is platform independent. All my tools are written in Node.

5. I need to test on IE from time to time

6. Did a mention OSX is really really awful? They should just give up writing software. I am waaaay more productive using Windows or Linux.

7. The Macbook display and hardware is beautiful. Even Windows 10 looks beautiful on it.

8. It’s dual boot. No Macbooks were actually harmed in the process.

So let’s get started:

Download Windows 10 ISO file from
https://www.microsoft.com/en-au/software-download/windows10ISO

You should choose the Windows 10 Anniversary Edition to install using Bootcamp. You can upgrade from that later.
i.e. Win10_1607_English_x64.iso

Run Bootcamp 
Set Partition size. Think about the right size now rather than trying to resize it later. Select the Windows iso
Format the BOOTCAMP partition 
Click next and let it do its thing

Windows will start, answer questions about preferred language etc
When asked answer “I don’t have a licence code” 
Install Windows 
Install Bootcamp inside windows

Make the fonts look the right size:

Control Panel > Display: Turn off custom scaling

Reduce sensitivity of trackpad for two finger scrolling:

Control Panel > Mouse > choose how many lines to scroll = 1

Select “additional mouse options” and set lines to scroll to 1

Reverse the scrolling direction for “natural” trackpad scrolling:

Use Regedit to swap direction of track pad:

Under

HKEY_LOCAL_MACHINESYSTEMCurrentControlSetEnumHIDVID_??????Device Parameters

(where ??? is different on every machine) search for properties named FlipFlopWheel and FlipFlopHScroll and set both of them to 1. They may appear a few times for different values of ???.

Turn background to black

(On an unregistered Windows, you are prevented from customising the wallpaper but they don’t stop you from blanking it out completely)

Control panel > ease of access > other options > show windows background = off

Optional: Make Command key work like the Control key

If you find yourself reaching for the left Command key a lot you might want to map it to the same value as the left Control key. The easiest way to do that is to use the free open source tool SharpKeys

http://www.randyrants.com/2006/07/sharpkeys_211/

Finishing up

Connect to WiFi 
Install Chrome 
Login to Chrome

My setup for web development:

Install Git Bash for Windows
> Use git from windows command prompt

Install VS Code 
Open command window Ctrl-`
On first run you’ll be asked if you want to customise command prompt 
Choose your preferred terminal shell 
Choose git bash

Install Node