I'm trying to write a JS class (using cutting-edge JS) that will render a guitar neck using HTML5. While I've written some old-school JS classes before, I understand that the latest(?) JS spec, ECMAScript 6, has the class keyword but that this is just "syntactical sugar over JavaScript's existing prototype-based inheritance." Should I use the old school way of defining a class:

var MyClass = function() {
	this.foo = "foo";
	this.bar = "bar";
};

Or should I use the new style?

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

I'm guessing it should be the new style and that some abominable tool set will use Gulp or something to compile my code back to the more-supported ECMAScript 5 type syntax.

Also, Is it déclassé to wrap my class definition inside an anonymous function?? It might start to look something like this:

(function(){

// object to encapsulate this module
var GuitarNeck = function() {
	this.foo = "foo";
	this.bar = "bar";
};



if(typeof window!="undefined"){
	window.MYNAMESPACE || (window.MYNAMESPACE = {});
	if(window.MYNAMESPACE.GuitarNeck){
		for(var prop in GuitarNeck){
			window.MYNAMESPACE.GuitarNeck[prop]=GuitarNeck[prop]
		}
	}else{
		window.MYNAMESPACE.GuitarNeck=GuitarNeck
	}
} else {
	throw "'window' not defined. Unable to attach GuitarNeck.";
}
})();
    Write a Reply...