OZONE Asylum
Forums
DHTML/Javascript
Instantiating a JavaScript object by calling prototype.constructor.apply
This page's ID:
30593
Search
QuickChanges
Forums
FAQ
Archives
Register
Edit Post
Who can edit a post?
The poster and administrators may edit a post. The poster can only edit it for a short while after the initial post.
Your User Name:
Your Password:
Login Options:
Remember Me On This Computer
Your Text:
Insert Slimies »
Insert UBB Code »
Close
Last Tag
|
All Tags
UBB Help
Thanks esskay, poi for looking into this. [b]esskay[/b]: I had tried intercepting the constructor like you did, to no avail. The reason is explained below. [b]poi:[/b]: Thanks for checking the spec. Any word from the devs? I've done more investigation of my own and came up with the conclusion that this is an impossible feat, due to how the Date class is implemented. I've inspected the SpiderMonkey source code to see how Date was implemented. I think it all boils down to the following few lines: [code] static JSBool Date(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { jsdouble *date; JSString *str; jsdouble d; /* Date called as function. */ if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) { int64 us, ms, us2ms; jsdouble msec_time; /* NSPR 2.0 docs say 'We do not support PRMJ_NowMS and PRMJ_NowS', * so compute ms from PRMJ_Now. */ us = PRMJ_Now(); JSLL_UI2L(us2ms, PRMJ_USEC_PER_MSEC); JSLL_DIV(ms, us, us2ms); JSLL_L2D(msec_time, ms); return date_format(cx, msec_time, FORMATSPEC_FULL, rval); } /* Date called as constructor. */ // ... (from here on it checks the arg count to decide how to create the date) [/code] When Date is used as a function (either as Date() or Date.prototype.constructor(), which are exactly the same thing), it defaults to returning the current time as a string in the locale format. I don't think there's anything that can be done at the JS level to circumvent this. And this is probably the end of my pursuit in this topic. I've also noticed something interesting: [code] /* Set the value of the Date.prototype date to NaN */ proto_date = date_constructor(cx, proto); if (!proto_date) return NULL; *proto_date = *cx->runtime->jsNaN; [/code] Date.prototype is a Date instance with the internal value of NaN and therefore, [code] alert(Date.prototype); // Always returns "Invalid Date" on FF, Opera, Safari, Chrome but not IE [/code] *SURPRISE* *SURPRISE* *SURPRISE* IE does things a bit differently and I think sets the internal value to -1 so that Date.prototype is a date slightly before epoch. *END SURPRISE* *END SURPRISE* *END SURPRISE* [url=http://magnetiq.com/][img]http://magnetiq.com/exports/oa_sig.gif[/img][/url] [small](Edited by [url=http://www.ozoneasylum.com/user/6682]MaGnA[/url] on 10-10-2008 05:19)[/small]
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »