Thanks guys. I am going to look over the functions above. I think I was trying to get a JS object that I could call directly instead of having to var Whatever = new Foo(); but looks like I am going to have to do that to get the best pattern.
thanks again. On May 12, 10:13 am, Ryan Gahl <[email protected]> wrote: > > var j = new (Class.create({initialize: function(name){ this.x = > > 5; }})); > > j.x; // 5 > > Sure, but for some reason I highly doubt that's what he was going for. > That's a non-reusable class definition, after all. He'd be much better off > just removing his "new" and creating the instance after the class > declaration, as he can then create other instances of the class (which is > most likely what he wants). > > Was trying not to confuse the guy with an advanced discussion like this. > "Just remove the 'new' keyword" probably served the purpose of helping him > get past his issue just fine, and represents a closer to real life > situation. No one creates instances like that, kangax, and you know it :) > > So yes... I'm wrong, kangax corrected me (again)... of course ALL functions > will work with the new keyword. But as I was saying, some are less useful as > constructors than others. And this "pattern", Louis, that kangax just gave > you... it's not a good pattern - please don't use it :)... you'd be better > off just doing "var j = {x: 5};" Kangax knew this, clearly... and he is > correct about the pattern being _possible_ -- but by all means don't do > that. > > On Mon, May 11, 2009 at 9:46 PM, kangax <[email protected]> wrote: > > > On May 11, 6:38 pm, Ryan Gahl <[email protected]> wrote: > > > Louis, > > > > Your error is that you should not be using the "new" keyword before > > > "Class.create()" > > > > Class.create() is a helper function that essentially just returns a > > > function. Note, in js, all named functions are considered constructors. > > > They don't really have to be named (i.e. have an identifier - optional > > in FunctionExpression and required in FunctionDeclaration). All > > Function objects have internal [[Construct]] method and so can be > > initialized with `new` (which happens to invoke that internal > > method) ;) > > > var j = new (function(name){ this.name = name; })('John'); > > j.name; // "John" > > > OP's issue is related to the fact that `new` operator has a higher > > precedence than a function call in an expression: > > > function F(){}; > > new F(); // [[Construct]]'s `F` > > > new (F()); // Calls `F`, then [[Construct]]'s *return value* of that > > function > > > So technically speaking, OP only needed to wrap `Class.create` > > expression with parenthesis, to make sure he's constructing return > > value of `Class.create`'s invocation and not `klass` function itself. > > > var j = new Class.create({initialize: function(name){ this.x = 5; }}); > > j.x; // undefined > > > var j = new (Class.create({initialize: function(name){ this.x = > > 5; }})); > > j.x; // 5 > > > [...] > > > -- > > kangax --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
