Unit 6. Classes

 

JavaScript is an object-oriented language. Except for a few primitives, most values in JavaScript, even functions, are objects. Most of the JavaScript code that gets written is for creating custom objects. Many times you’re probably building your own objects from scratch, but sometimes you’ll be starting from a base object provided from a framework or library and adding custom functionality to it. Unfortunately, there hasn’t been a clear way for library authors to provide a base object prototype that can be extended by application developers. This has left many library authors needing to reinvent the wheel.

But nobody reinvents the wheel the same way. Here’s how Backbone.js provides a way to extend base objects:

Backbone.Model.extend({
  // ... new methods
});

And here’s how it’s done with React.js:

React.createClass({
  // ... new methods
});

Backbone provides a method called initialize that acts as a pseudo constructor function. React does not. But React does autobind its methods when they’re created with createClass. And this is just the beginning of the differences: we quickly compared two here, but there are dozens of JavaScript frameworks that provide a base object, which means that you’d need to remember all the subtle differences of how different classes work depending on what library or framework you’re using.