Basics
const
const PROG_VERSION = "version 0.0.a.0"
Date
function PerformanceCounter()
{
this.baseCount = 0;
this.lapCount = 0;
this.clear = function() {
this.baseCount = 0;
this.lapCount = 0;
};
this.start = function () {
this.baseCount = new Date();
this.lapCount = this.baseCount;
};
this.lap = function() {
var lap = new Date();
var elapsed = lap - this.lapCount;
this.lapCount = lap;
return elased;
}
this.stop = function() {
var lap = new Date();
return (lap - this.baseCount);
}
}
継承
function extend(s, c)
{
function f(){};
f.prototype = s.prototype;
c.prototype = new f();
c.prototype.__super__ = s.prototype;
c.prototype.__super__.constructor = s;
c.prototype.constructor = c;
return c;
};
ButtonBase = function() {
console.log("ButtonBase");
this.sayA = function() { console.log("A"); };
};
ButtonA = extend(ButtonBase, function() {
this.__super__.constructor();
console.log("ButtonA");
this.sayB = function() { console.log("B"); };
});
var o = new ButtonA();
o.sayA();
o.sayB();
引数
引数の数を検査する。
if ( arguments.length >= 1 ) {
...
}
引数の数で処理を変える。
function MyObj() {
this._prop = {};
this.set = function() {
switch(arguments.length) {
case 1:
var spec = arguments[0];
for(name in spec) {
this._prop[name] = spec[name];
}
break;
case 2:
this._prop[arguments[0]] = arguments[1];
break;
}
};
this.get = function(name) {
return this._prop[name];
};
}
MyObj myObj = new MyObj();
myObj.set("a", "1");
myObj.set({ a: 1, b: 2, c: 3});