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
Very interesting topic. I've been struggling to wrap my head around JS OOP for some time now - It seems pretty wonky compared to other high level languages. Anyway, I've been searching google and experimenting for about the last hour trying to find something. The best I could come up with is that I think prototype.constructor is not the right thing. There is a method called constructor that does return some weird garbage that appears to be related to initialization, however I don't believe that it is the actual constructor. I think it's too late after you've called new ObjectName() because the constructor has already been invoked: you can't change it now. So I don't have a direct answer for you because I don't yet understand it fully, but I can suggest this different angle of attack: try changing the definition of the Date object BEFORE instantiating it. Something like this: [code] // The problem here is that constructor seems to not be the correct method // to override the actual class constructor - so what is the right way? Date.prototype.constructor = function(myargs) { // This calls the original constructor with myargs as an array, just like you requested Date.apply(this, myargs); } var comps = [ 2008, 10, 8, 00, 16, 34, 254 ]; // NOW we try to instantiate a Date object after we've altered the behavior through the prototype... var d = new Date(comps); var year = d.getYear(); var month = d.getMonth(); var day = d.getDate(); [/code] To be clear: the above does not work, but I think the approach is worth looking at... Regards, - SK
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »