On Mar 8, 8:58 pm, "dzw!" <[email protected]> wrote:
> Tobie, thanks for answer it is helpful but still what I am looking for
> is practical advice how do you handle it in Prototype.
>
> Just to clarify:
>
> //this is what I would do without using Prototype
>
> function PureClass(){};
> var wow = new PureClass();
> wow.constructor;  // and I would get PureClass
>
> // in Prototype the same
>
> var PureClassPrototype = Class.create();
> var wow_prototype = new PureClassPrototype();
> wow_prototype.constructor; // here I get not very helpfull klass() -
> ok it is helpfull and much better than nothing :)
> wow_prototype.constructor == PureClassPrototype;  // true
>
> So I am maybe wrong but in JS I get what I want, but in Prototype my
> guess is that there is some other way[i have few ideas how to go
> around that but I am quite sure It is already solved]..

It's not about "JS" or Prototype : )

`Class.create` knows nothing about a variable that you assign its
return value to, so obviously, it can not declare a function with the
"name" matching the name of that variable. This is just how ECMAScript
is designed - expression on a right hand side of an assignment is not
aware of its left hand side "context".

In the former example you declare a function. When you declare a
function, whatever comes after "function" keyword and before opening
parenthesis is a function Identifier. That Identifier is what you see
when "inspecting" a function. `Class.create` is not aware of which
variables it is being assigned to and even if it's being assigned to
something at all (e.g. it could simply be called with a `new` operator
- `new Class.create({})`). That is why `Class.create` always declares
a "klass" function internally and it is that function that is being
returned as its resulting value.

Another thing worth mentioning is that function Identifier can not be
set from a string value, without using `eval`. This means that even if
you passed some name as a string value to `Class.create`,
`Class.create` would not be able to declare a function with an
Identifier matching that name unless it resorted to using `eval`: -

var name = 'boo';
eval('function ' + name + '(){}')`; // function boo(){}

You can always assign the name as a property of constructor's
prototype explicitly and then use it for whatever purposes:

var Foo = Class.create({
  __id: 'Foo',
  ...
});

(new Foo()).__id; // 'Foo'

or something like that...

You can, of course, always create constructors and their prototypes
manually instead of using `Class.create`.

[...]

--
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to