I found this widget online that I am trying to learn how to write in the latest ES6 style. The javascript was minified but I imagine it was originally written in ES6.
So the basic structure of it looks like this:
Some_config = {};
(function() {
var t, e = function(t, e) {
return function() {
return t.apply(e, arguments)
}
},
i = [].slice;
t = function() {
function t() {
this.log = e(this.log, this);
var t;
t = "undefined" != typeof Some_config && null !== Some_config ? Some_config : {},
"undefined" != typeof Some_config && null !== Some_config && this.init(t)
}
return t.prototype.pushMessage = function(t, e) {
return null == e && (e = {}), this.elements.frame.contentWindow.postMessage([t, e], "*")
},
t.prototype.log = function() {
if (this.localOptions.debug)
return console.log("[Debug]", arguments)
},
t.prototype.warn = function(t) {
return console.warn("[Error]", t)
},
t.prototype.init = function(t) {
var e;
try {
this.nextInit(t)
} catch (t) {
e = t, this.warn(e)
}
return this
},
t.prototype.nextInit = function(t) {
console.log('calling nextInit');
},
t
}(), window.SomeApi = new t
}).call(this);
So this javascript runs in a browser, so it looks like it is a immediately invoked but then it calls call(this)
. What exactly is going on in the last 2 lines?
}(), window.SomeApi = new t
}).call(this);
The style in general looks very foreign to me, is this because it was minified from the original ES6 style?
If this was written as a ES6 class, what would it look like structure wise? I'm hoping it would look cleaner and it would be easier for me to learn/build from.
class SomeApi {
constructor() {
}
log() {
if (this.localOptions.debug)
return console.log("[Debug]", arguments)
}
init(t) {
var e;
try {
this.nextInit(t)
} catch (t) {
e = t, this.warn(e)
}
return this
}
}