# Define my class class myClass # Define a method within my class doSomething: -> console.log("Hello world!") # Add to exports for node, or window for browser if typeof module isnt 'undefined' and module.exports module.exports = new myClass() else this.myClass = new myClass()
First, we create a class named "myClass" and add a method to it. Then, with a simple if statement, we can determine whether we’re running this code in Node.js or in the browser and export the class accordingly. Let’s assume we’re compiling this down into "my-class.js".
In Node.js, we would use the class like this:
myClass = require("my-class") myClass.doSomething()
In the browser, we would use the class like this: