Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2949
Abstract
alue</li><li>The JS engine asks the <code>car.prototype</code> object for a value with the key <code>start</code>.</li><li>the <code>car.prototype</code> returns the <code>start</code> function, that the JS engine executes immediately.</li></ol><p id="2d4f">Accessing the make and model properties are performed similarly, except they are defined on the car object directly, instead of the prototype.</p><p id="5106">Inheritance is a bit trickier. It is handled on lines 24–25. The most important function here is the <code>Object.create</code> function. It accepts an object and returns a brand new one, with its prototype set to whatever was passed as an argument. Now, if the JS engine does not find a value on the <code>sportsCar</code> object or <code>sportsCar.prototype</code>, it will consult <code>sportsCar.prototype.prototype</code> which is the prototype of the <code>Car</code> object.</p><h1 id="d41b">ES6 Class keyword</h1><p id="f217">With the release of ES6 in 2015, the long-awaited <code>class</code> keyword arrived in JavaScript. It was done as per numerous requests by the community because people were feeling uncomfortable coming from object-oriented languages. But they missed one important point.</p><blockquote id="73ac"><p>JavaScript has no idea what classes are</p></blockquote><p id="eef7">JavaScript is not an object-oriented language, it was not designed to be one, the notion of classes is absolutely not applicable to it. While everything in JS is indeed an object, these objects are different from the ones in Java or C#. In JS, an object is simply a Map data structure with a somewhat <a href="https://readmedium.com/3-different-kinds-of-prototypal-inheritance-es6-edition-32d777fa16c9">sophisticated lookup procedure</a>. That is it, really. And when I say everything is an object, I mean it: even functions are objects. You can check it with this snippet:</p>
<figure id="5dcb">
<div>
<div>
<iframe class="gist-iframe" src="/gist/r3dm1ke/ea4c332306802dd406a03cdf19889023.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="972b">Ok, this is all good, but how does the <code>class</code> keyword work then? Glad you asked. Do you remember the <code>Car</code> and <code>SportsCar</code> example earlier? Well, the class keyword is simply syntactic sugar on top of that. In other words, class produces conceptually the same code and serves only aesthetic and readability purposes. As I promised earlier, here is an example of these same classes in ES6:</p>
<figure id="37cc">
<div>
<div>
<iframe class="gist-iframe" src="/gist/r3dm1ke/45025f5ff6bebad27005b7dcd46f1967.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</
Options
div>
</div>
</figure></iframe></div></div></figure><p id="642c">These examples are identical and produce the same result. What is interesting, they produce (almost) the same code under the hood. I will not write it out here, but if you are curious, head out to the <a href="https://babeljs.io/en/repl#?browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=MYGwhgzhAEDCYCdoG8BQ1rAPYDsIBcEBXYfLBACgFswBrAUwBpoqsATekAShXQ2nwALAJYQAdDQbQAvCzr0A3HwxDRE9pxksNIJRgC-fPgUT4KPNP0y4IWEPTEgsAcwoByAG4IsWKm6560IYYfGQAyoTCOK4WytZ4dg5OrgAG8EgAtNAAJMiq4pL0-tBZufnqHCD6KQF8hoaooJAwYQAO5PgQ6dD0AB749DhsMN2W8QTEpOTU8syslcz4RAgARljAgojO9GyxVhBErfSUhXM6tVblS6vrmwjbbFrXaxtbO4HB0Mb4pua8VtgEvZHC53AA1ABKAHkYVCALL-D6oBqoAD0qOgAEFSEQwCBoEQIGBttAEPQaFEYEJ6NAiVR6KgPIhMMzZDh6AB3OCIdwAOVERJwbmYbjCRBwOAAnojGogxCYEGYAtB0dAvD4qI0bIkQa5gHLCsrVfyoGAcKhGcyIO1FV1WdB2Vy2h07ZRRUQVogiMLoG4AEIQgBaPsIRHotWtLvS8p-ivMChVGMhsPhWqBSVB-oQYmetzeuwTqtD9CAA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=true&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015&prettier=true&targets=&version=7.8.0&externalPlugins=">online Babel transpiler</a> and have a look at the output.</p><h1 id="36db">Why not?</h1><p id="7228">Now you should have an understanding of what <i>classes</i> in JS are and how they work. Now, with all this knowledge, I can explain why using classes in JS is a bad idea.</p><ol><li><b>Binding issues</b>. As class constructor functions deal closely with <code>this</code> keyword, it can introduce potential binding issues, especially if you try to pass your class method as a callback to an external routine (hello, React devs 👋)</li><li><b>Performance issues</b>. Because of classes’ implementation, they are notoriously difficult to optimize at runtime. While we enjoy performant machines at the moment, the fact that <a href="https://en.wikipedia.org/wiki/Moore%27s_law">Moore’s law is fading away</a> can change all that.</li><li><b>Private variables</b>. One of the great advantages and the main reasons for classes in the first place, private variables, is just non-existent in JS.</li><li><b>Strict hierarchies</b>. Classes introduce a straight top-to-bottom order and make changes harder to implement, which is unacceptable in most JS applications.</li><li><a href="https://reactjs.org/docs/hooks-intro.html"><b>Because the React team tells you not to</b></a>. While they did not explicitly deprecate the class-based components yet, they are likely to in the near future.</li></ol><p id="97ac">All of these issues can be mitigated with JS objects and prototype delegation. JS offers so much more that classes can ever do, yet most developers are blind to it. If you want to truly master JS, you need to embrace its philosophy and move away from dogmatic class-based thinking.</p></article></body>