From 21648db7327dc5d638928cd9dbcc85a001d989f6 Mon Sep 17 00:00:00 2001 From: Gabriel Kunkel Date: Thu, 28 Jan 2016 11:00:12 -0800 Subject: [PATCH] exercise 19 switched Object.create() over to Object.assign() --- index.html | 60 ++++++++++++++++-------------------------------------- 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/index.html b/index.html index e9700e2..16df4d8 100644 --- a/index.html +++ b/index.html @@ -2146,29 +2146,15 @@

Exercise 19: Reducing with an initial value

// ] return videos. reduce(function(accumulatedMap, video) { + var obj = {}; - // Object.create() makes a fast copy of the accumulatedMap by - // creating a new object and setting the accumulatedMap to be the - // new object's prototype. - // Initially the new object is empty and has no members of its own, - // except a pointer to the object on which it was based. If an - // attempt to find a member on the new object fails, the new object - // silently attempts to find the member on its prototype. This - // process continues recursively, with each object checking its - // prototype until the member is found or we reach the first object - // we created. - // If we set a member value on the new object, it is stored - // directly on that object, leaving the prototype unchanged. - // Object.create() is perfect for functional programming because it - // makes creating a new object with a different member value almost - // as cheap as changing the member on the original object! - - var copyOfAccumulatedMap = Object.create(accumulatedMap); - - // ----- INSERT CODE TO ADD THE VIDEO TITLE TO THE ---- - // ----- NEW MAP USING THE VIDEO ID AS THE KEY ---- - - return copyOfAccumulatedMap; + // ----- INSERT CODE TO ADD THE VIDEO TITLE TO THE ---- + // ----- NEW MAP USING THE VIDEO ID AS THE KEY ---- + + // Object.assign() takes all of the enumerable properties from + // the object listed in its second argument (obj) and assigns them + // to the object listed in its first argument (accumulatedMap). + return Object.assign(accumulatedMap, obj); }, // Use an empty map as the initial value instead of the first item in // the list. @@ -2230,28 +2216,16 @@

Exercise 19: Reducing with an initial value

// ] return videos. reduce(function(accumulatedMap, video) { + var obj = {}; + + // ----- INSERT CODE TO ADD THE VIDEO TITLE TO THE ---- + // ----- NEW MAP USING THE VIDEO ID AS THE KEY ---- + obj[video.id] = video.title; - // Object.create() makes a fast copy of the accumulatedMap by - // creating a new object and setting the accumulatedMap to be the - // new object's prototype. - // Initially the new object is empty and has no members of its own, - // except a pointer to the object on which it was based. If an - // attempt to find a member on the new object fails, the new object - // silently attempts to find the member on its prototype. This - // process continues recursively, with each object checking its - // prototype until the member is found or we reach the first object - // we created. - // If we set a member value on the new object, it is stored - // directly on that object, leaving the prototype unchanged. - // Object.create() is perfect for functional programming because it - // makes creating a new object with a different member value almost - // as cheap as changing the member on the original object! - - var copyOfAccumulatedMap = Object.create(accumulatedMap); - - copyOfAccumulatedMap[video.id] = video.title; - - return copyOfAccumulatedMap; + // Object.assign() takes all of the enumerable properties from + // the object listed in its second argument (obj) and assigns them + // to the object listed in its first argument (accumulatedMap). + return Object.assign(accumulatedMap, obj); }, // Use an empty map as the initial value instead of the first item in // the list.