[Proto-Scripty] Re: Class.create and klass() how do you use it folks?

2009-03-09 Thread dzw!

Thank You guys for so much help, it saved me heaps of google time:)
I got much deeper insight how things work in js now.

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.  - that already became tattoo
in my brain;)

the rest hints I will need to digest a little bit, anyway thank you.

The root of my question was issue with Apress book, its Chapter 13,
Configurable mixins example it didn't work,
It was rather small thing:  klass.DEFAULT_OPTIONS instead chain
[i].DEFAULT_OPTIONS but for newbie was enough to get confused.

http://pastie.org/411558

dzw!


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



[Proto-Scripty] Re: Class.create and klass() how do you use it folks?

2009-03-08 Thread Tobie Langel

Hi,

JavaScript, as a language doesn't have the self-reflexiveness you are
looking for.

There are different hacks to find this kind of information, but imho
they go against the dynamic and prototypical nature of the language.

When you do:

var Foo = Class.create({});

what you are actually doing is creating a constructor (a function
declaration contained in the closure created by Class.create), which
you then assign to the Foo variable. (In prototype, that constructor
happens to be called klass, but it could be called anything else,
that wouldn't change anything).

It would now be very well possible to do:

var Bar = Foo;

and use:

new Bar();

to create a new Foo object.

If the classes you create are in the global scope, you can always
iterate over the global object (window, or this) to find the name of
the variable that points to your constructor object. But apart from
debugging purposes, there is little use for that.

Hope this clarifies your issue.

Best,

Tobie


On Mar 8, 12:19 pm, dzw! bartoszw...@googlemail.com wrote:
 Houston, new Prototype user has a problem:

 var SimpleClass = Class.create({})
 var xxx = new SimpleClass();

 xxx.constructor == SimpleClass // i got true

 xxx.constructor // i got klass()

 So what I am looking  is: how to get the constructor name? the same is
 with Prototype's superclass and subclasses.. I tried to google it but
 with no success.

 Any hint? What is about that klass() thing?
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Class.create and klass() how do you use it folks?

2009-03-08 Thread Tobie Langel

When you say:

 function PureClass(){};
 var wow = new PureClass();
 wow.constructor;  // and I would get PureClass

What you really mean is that, in Firebug, you get the string:
PureClass() displayed, correct?

That's because Firebug takes advantages of the non-standard `name`
property of function objects implemented by Firefox. This property
gets populated with the name of the function declaration. It's
specification in ES 3.1 is currently being discussed[1].

I suppose that your main concern is for debugging purposes, isn't that
correct? You might be interested in reading kangax's post[2] on a
related subject, which could solve part of your problem.

If not, you can use uglier solutions (as long as you know the
namespace in which you've declared your classes):

function inspect(klass) {
  var namespace = window; // or myLib or whatever
  for (var prop in namespace) {
if (namespace[prop] === klass) {
  return prop;
}
  }
  throw Can't find class in  + namespace;
}

Best,

Tobie

[1] https://mail.mozilla.org/pipermail/es-discuss/2009-March/008905.html
[2] http://thinkweb2.com/projects/prototype/semantic-constructors/


On Mar 9, 1:58 am, dzw! bartoszw...@googlemail.com 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]..

 Or if I am going in very bad direction and what I am trying is
 useless,stupid etc ..  please let me know.. :)

 Same is for e.g with Class.subclasses , I got Array of [ klass(), klass
 () ] and than all I can do is '==' against Something, what I am
 looking for is to have in that case array of [Something,
 SomethingElse ]

 Thanks

 dzw!

 On Mar 9, 1:08 am, Tobie Langel tobie.lan...@gmail.com wrote:

  Hi,

  JavaScript, as a language doesn't have the self-reflexiveness you are
  looking for.

  There are different hacks to find this kind of information, but imho
  they go against the dynamic and prototypical nature of the language.

  When you do:

      var Foo = Class.create({});

  what you are actually doing is creating a constructor (a function
  declaration contained in the closure created by Class.create), which
  you then assign to the Foo variable. (In prototype, that constructor
  happens to be called klass, but it could be called anything else,
  that wouldn't change anything).

  It would now be very well possible to do:

      var Bar = Foo;

  and use:

      new Bar();

  to create a new Foo object.

  If the classes you create are in the global scope, you can always
  iterate over the global object (window, or this) to find the name of
  the variable that points to your constructor object. But apart from
  debugging purposes, there is little use for that.

  Hope this clarifies your issue.

  Best,

  Tobie

  On Mar 8, 12:19 pm, dzw! bartoszw...@googlemail.com wrote:

   Houston, new Prototype user has a problem:

   var SimpleClass = Class.create({})
   var xxx = new SimpleClass();

   xxx.constructor == SimpleClass // i got true

   xxx.constructor // i got klass()

   So what I am looking  is: how to get the constructor name? the same is
   with Prototype's superclass and subclasses.. I tried to google it but
   with no success.

   Any hint? What is about that klass() thing?
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Class.create and klass() how do you use it folks?

2009-03-08 Thread kangax

On Mar 8, 8:58 pm, dzw! bartoszw...@googlemail.com 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 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
-~--~~~~--~~--~--~---