instantiating javascript vigilant job prototype.constructor.apply
let me start specific instance i'm perplexing do.
i have an array year, month, day, hour, minute, second millisecond components form [ 2008, 10, 8, 00, 16, 34, 254 ]
. i'd instantiate date vigilant controlling following customary constructor:
new date(year, month, date [, hour, minute, second, millisecond ])
how i pass array constructor new date instance? [ update: doubt indeed extends over specific example. i'd whole fortitude built-in javascript classes date, array, regexp, etc. whose constructors over reach. ]
i'm perplexing something following:
var comps = [ 2008, 10, 8, 00, 16, 34, 254 ];
var d = date.prototype.constructor.apply(this, comps);
i substantially need "new
" there somewhere. above only advantage tide i called "(new date()).tostring()
". i also acknowledge i competence totally wrong instruction above :)
note: eval()
accessing array equipment one, please. i'm graceful certain i should means array is.
update: offer experiments
since means adult operative answer yet, i've finished some-more enactment around. here's new discovery.
i possess class:
function foo(a, b) {
this.a = a;
this.b = b;
this.tostring = duty () {
relapse this.a + this.b;
};
}
var foo = new foo(1, 2);
foo.prototype.constructor.apply(foo, [4, 8]);
document.write(foo); // advantage 12 -- yay!
but doesn't work unparalleled date class:
var d = new date();
date.prototype.constructor.call(d, 1000);
document.write(d); // still advantage tide :(
neither does work number:
var n = new number(42);
number.prototype.constructor.call(n, 666);
document.write(n); // advantage 42
maybe only isn't illusory unparalleled objects? i'm contrast firefox btw.
Comments
Post a Comment