Carbon.js makes it simple to calculate emissions. Its design is similar to the Ruby carbon gem. For any class that represents an “emission activity”, e.g. an automobile trip, you can extend it with Carbon.js to give it the ability to perform calculations.
Let’s look at an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
As you can see, the configuration defined in the
Carbon.emitter call maps properties on an instance of RentalCar to characteristics defined in automobile’s characteristics API. In order to calculate emissions, a call to
getEmissionEstimate with a callback function parameter is required. Within that callback, you can update HTML on a page with the result.
- Carbon only maps properties of an object to characteristics. It will be simple to allow an instance function to be mapped to a characteristic.
- Carbon.js requires that you use jQuery. One improvement would be to use platform-agnostic XHR and proxy functions, or intelligently detect available libraries, like Prototype, MooTools, etc.