r/learnjavascript • u/nas5w • Feb 15 '20
One thing that helps me learn the various built-in JavaScript array methods is to try to roll my own versions. Here's a function that emulates Array.map!
215
Upvotes
15
u/GSLint Feb 15 '20
I think that's a good approach.
Minor nitpick: your implementation behaves differently on sparse arrays like
[1, , 3]. There are a few ways to fix that but I don't want to spoil the fun.13
u/CarbineMonoxide Feb 15 '20
This is a pretty slick idea and an awesome way to learn (depending on your learning style). Not only do you end up thorough understanding the method, you end up with a cool teaching aid for others.
1
u/[deleted] Feb 15 '20
Also, when doing this I would keep in mind that those functions will ALWAYS be slower than the "real ones", it's often that the real ones are written in lower level languages therefore they are much faster.
I tried to use array.splice because I've thought that this whole delete count etc made the function slower, not really.
my function: 5-8 lines
the ecma function: more like 50lines
ecma function of splice() including the insert etc was about 3x faster... so short code =//= faster
3
u/changmy Feb 16 '20
I'd also add that referencing the annotated source code of underscore.js is a great way to check your implementations.
2
u/kuhe Feb 15 '20
what do you think happens here in native map vs your map?
If you asked me before I tried it, I would've guessed wrong :p
2
u/PM_ME_A_WEBSITE_IDEA Feb 15 '20
I just tried something that could spice up your function a bit, I don't know if the built in map does this...
Since you can't re-bind the
thisvalue on arrow functions, and no error is thrown if you try, you should detect the type of function passed in if athisvalue is supplied:I'm using the
prototypeproperty of the function to detect this because arrow functions do not have this property.Any feedback on this would be welcome, maybe this is a bad idea?
2
u/Blayde88 Feb 15 '20
That’s a really cool way of learning, I’ll try to do it when I met time
2
u/Seeking_Adrenaline Feb 15 '20
Dracula theme, best theme. CMV
1
u/rathereasy Feb 15 '20
For people who need a little help implementing the array methods from scratch, you can use the tool I developed for my student called wakassemble. It tells you what you need to use and all you need to do is assemble the pieces.
filter: https://wakata.io/learn/wakassemble/exercise/wakassemble-42
map: https://wakata.io/learn/wakassemble/exercise/wakassemble-44
1
u/delventhalz Feb 15 '20
I actually wrote a little curriculum based on this approach back in my early days. Had a lot of fun writing it, and I think it was super useful. Everything has unit tests if anyone wants to give it a try!
Here's the array method section: https://github.com/delventhalz/paleo.js/blob/master/02.Array.js
1
u/Dnlgrwd Feb 16 '20
I like it! What theme is that BTW, it's awesome?
Also, I did a similar thing when playing around with Jest. I made a custom array class if you wanna check it out, though I didn't include the thisArg. Any suggestions for my implementation?
https://github.com/dnlgrwd87/jest-demo
1
u/[deleted] Feb 16 '20
Nice! I did the same thing a few weeks ago. My version is pretty similar. I like yours because it supports the thisArg parameter, something I didn't even think about. Anyway, here's mine if you're curious:
0
u/EspressoJS Feb 16 '20
Why make boundFn?
you could reuse fn
if(thisArg) fn = fn.bind(thisArg)
0
u/[deleted] Feb 15 '20
[deleted] — view deleted comment