New Keyword Protection!

As mentioned before, constructor functions work pretty well. One instance where you can run into trouble though is if the function is invoked without the new keyword. When this happens instead of creating a new object the function behaves like a regular function and adds the properties to the global object–in browsers this would be window.

Final thoughts

In closing, ES6 classes provide an easy way to write object-oriented style javascript. When comparing to the options we had with ES5 and earlier, ES6 classes provide a clean and concise way to write object-oriented JavaScript. If you have used OOP in JavaScript with ES6 classes and would like to share what you’ve done, tweet a link to a gist or repo and use the hashtag #JavaScriptJanuary to let us know!