The Classy Way to Create Objects in JavaScript! 🐞
Hi, *|USERNAME|*!
One of the biggest (and most talked about) changes made to JavaScript has to do with how you create objects. In ES6, support for the class syntax was introduced. Instead of fiddling with Object.create, understanding how prototypes worked, and so on, you now had a much simpler way of creating objects without worrying about the messy details. You just defined your class and created a new object that relied on it:
class Planet {
constructor(name, radius) {
this.name = name;
this.radius = radius;
}
}
varmyPlanet = new
Planet("Earth", 6378);
console.log(myPlanet.name); // Earth
The end result was a new object. That's it. Since it's good to know what goes on under the covers, this is what you would see:
If all of this looks familiar to you, then you are in great shape. If this is new to you or you need a refresher, check out my The Classy Way to Create Objects in JavaScript tutorial for the low-down. For a more general introduction to JavaScript, keep this link handy as well.
TILL NEXT TIME!
As always, before I leave you to it, I would love to hear from you on things I can do better, topics you'd like me to write about, and more. Don't be shy! The easiest way to contact me is via Twitter, Facebook, or by posting on the forums (just like the good old days when you'd spend hours waiting for everyone's Flash-based signatures to load)!
Cheers,
Kirupa :-)