Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialcyber voyager
8,859 PointsCould someone explain the code on the video
Hello, I understand 80% of the code shown in the video but I still feel that I miss something important, I don't completely understand why would I want to use the new keyword. I read on the MDN about the new keyword and it says that it creates a new object... Why? I saw code being like this
new Map();
So what are we doing here since Map() is an object too? Creating an object with ... I am confused :P
If someone could enlighten me I would be grateful. Thank you very much Stavros
2 Answers
Jawann Carmona
19,556 PointsMap() is an object but if offers more options. Most importantly, in a plain object, your key value would have to be a string but in the Map, your key can be a function, Date(), Symbol, or even a DOM element. It also has a ton of methods that make iterating over complex data structures easier. Check out this (link for more info)[https://ponyfoo.com/articles/es6-maps-in-depth].
Jawann Carmona
19,556 PointsI think of it as an instance of an object prototype created by Javascript already. You are just calling a new instance of it and adding your own values. Say you created an object like a Car.
function Car(brand, model, year) {
this.brand = brand;
this.model = model;
this.year = year;
}
You would then call an instance of that object later using:
var coolCar = new Car(Honda, Civic, 2002);
So you are using the new keyword to call an instance of an Prototype - in this case - Map(). that exists globally in Javascript. It's like using any of the array functions that are part of the Array.prototype. Those things just exist in Javascript to be called.
cyber voyager
8,859 PointsOk, thank you very much. So what we are doing up there on my code is we are making an instance of an object and setting the Map to it. But why set Map? just so we can get the functionalities that it provides us? where are we going to place the name of our new instance? Thank you, Jawan Carmona.
cyber voyager
8,859 Pointscyber voyager
8,859 PointsThank you for your answer, the thing that I still don't understand is why would we pair new with Map() ? both create object's, I am confused with new, I know it creates some sort of instance but I don't fully understand it. Thank you