Heya, > You may want to use Crockford's factory pattern, rather than using any > sort of constructor: > > function specialDateFactory() { > var that = new Date(); > > that.isEaster = function () {return ...}, > that.isLaborDay = function () {return ...}, > > return that; > > }
The problem with that as written is that it creates separate isEaster and isLaborDay functions for every single instance you create, each of which eats memory: var a = specialDateFactory(); var b = specialDateFactory(); alert(a.isEaster === b.isEaster); // "false" You can avoid that: var MoreDateFunctions = { isEaster: function () {return ...}, isLaborDay: function () {return ...} }; function specialDateFactory() { return Object.extend(new Date(), MoreDateFunctions); // Or, without Prototype's Object.extend: // var fname, that; // that = new Date(); // for (fname in MoreDateFunctions) { // that[fname] = MoreDateFunctions[fname]; // } // return that; } ...but I'm not seeing much benefit at that point. I suppose it means you haven't modified the Date prototype. (BTW: Anyone tempted to create an ExtendedDate object using a Date object as its prototype, think again, for some reason the spec specifically disallows it in Section 15.9.5, and Firefox [and probably others] implements the limitation.) -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On May 20, 4:07 pm, "P. Douglas Reeder" <reeder...@gmail.com> wrote: > You may want to use Crockford's factory pattern, rather than using any > sort of constructor: > > function specialDateFactory() { > var that = new Date(); > > that.isEaster = function () {return ...}, > that.isLaborDay = function () {return ...}, > > return that; > > } > > You can get more sophisticated - creating a prototype object for > special dates, with the functions defined on it, and using closures to > protect it, but I can't write that off the top of my head... > > On May 20, 7:50 am, ColinFine <colin.f...@pace.com> wrote: > > > On May 20, 12:16 am, RobG <rg...@iinet.net.au> wrote: > > > > On May 19, 9:26 pm, ColinFine <colin.f...@pace.com> wrote: > > > > > This is a case when you really truly want just the facilities that > > > > Javascript provides (prototypes rather than classes), and using > > > > (pretend) classes makes it harder not easier. > > > > Yes, although some may argue that it is better to create a new object > > > that has the required methods and leverages the methods of Date, > > > rather than modifying a built-in object prototype, > > > Fair comment. But you are still talking about using Javascript's > > prototype, rather than inventing classes. That was my point. > > (I wouldn't have made this remark before I read Crockford's > > "Javascript: The Good Parts" - that's opened my eyes to the strengths > > that JS has. ) > > > Colin > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---