Is it possible to create JavaScript class without using 'this' keyword? Expressions like
var self = this;
...
self.property = value;
is also prohibited. I dont want to think about context and use only vars.
Take a look at this simple class:
function Timer() {
var interval = 0;
var count = ++Timer.totalCount;
function setInterval(val) {
interval = val;
}
function run() {
setTimeout(done, interval);
}
function done() {
var div = document.createElement('div');
div.innerHTML = 'Timer #' + count + ' finished in ' + interval + ' ms';
document.body.appendChild(div);
}
return {
setInterval: setInterval,
run: run
}
}
Timer.totalCount = 0;
var t1 = new Timer();
var t2 = new Timer();
var t3 = new Timer();
//how to directly set interval value without set function?
t1.setInterval(3000);
t2.setInterval(2000);
t3.setInterval(1000);
t1.run();
t2.run();
t3.run();
This class does not use 'this' at all.
Is it correct?
Can I create real classes like that? Is there potential problems?
How to create property in this class without get/set function?
Is that class possible to extend using inheritance?