On May 11, 6:38 pm, Ryan Gahl <ryan.g...@gmail.com> 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 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to