Class
How do we write OO Javascript today?
var Model = function(name, options) {
this.name = name;
this.options = options;
};
var someModel = new Model("Some model", { option: 1 });
So, basically just a function..
(function(ns){
var model = function() { };
ns.Model = model;
})(window.ns = window.ns || {});
So, basically just a function..
var Parent = function() { };
var Child = function() { };
Child.prototype = new Parent(); // wut?
var child = new Child();
var Parent = function() { };
var Child = $.extend({}, Parent);
// or
var Child = Parent.subclass({});
// or
var Child = beget(Parent, {});
// and they all pretty much do
_child.prototype = _parent.prototype;
return new _child();
var Child = function() {
// Constructor
Parent.call(this);
};
Child.prototype.someMethod = function() {
// Method
Parent.prototype.someMethod.apply(this, []);
};
"This is why you should favour composition over inheritance!"
class Model {
constructor(){
this.name = name;
this.options = options;
}
};
var someModel = new Model("Some model", { option: 1 });
// module1.js
export class Model {
}
class PrivateModel {
}
// module2.js
import { Model } from 'module1.js'
Namespacing is a part of the problem, not the solution.
class Parent {
}
class Child extends Parent {
}
var child = new Child();
Sane inheritance syntax.
Still prototypal inheritance under the hood.
class Child extends Parent {
constructor() {
super();
}
someMethod () {
super();
}
}
Officially.. Well.. No one knows.
$ npm install traceur
$ traceur --out output/module.js --script/module.js
Grunt, gulp or build server.