One thing you may also want to consider is writing less C++ code. It can be
tempting to write your entire object in C++ but often times it's easier,
faster, and more efficient to write everything you possibly can in JS and
go to C++ only when you have to. I don't know your application but it's
worth considering!

On Sun, Feb 26, 2012 at 7:14 AM, FTPlus <[email protected]> wrote:

> Hey,
> Thanks for your time, I've figured it out. It might be a bit hackish
> but I found a way of doing it.
>
> First I make a regular class template (that is a function template
> with a prototype) for the Cage.
> I make a new object that will become the prototype for the Bird
> instances. It will be stored in a persistent handle.
>
> Then in the constructor of the Cage I will create a function template
> for the Bird and assign a function instance. I will set the parent
> cage object (args.This() in the Cage constructor) as a hidden value of
> the function instance assigned.
> Now in the Bird constructor function I will retrieve the hidden value
> I set by first getting the "constructor" object. So without any safe
> guards it will look like this:
>
> Handle<Object> parent = args.This()->Get(String::New("constructor"))-
> >ToObject()->GetHiddenValue(String::New("cage"))->ToObject();
>
> The last thing I do is set the prototype to the one stored in the
> persistent handle.
>
> It works, I don't know if there are better ways to do it but for now
> I'm satisfied. :)
>
> - Ferry
>
> On Feb 23, 4:49 pm, FTPlus <[email protected]> wrote:
> > Hey,
> > I'm working on several programs that use v8 as front-end and I'm
> > trying to present my bindings as natural as possible to the users. I
> > must say I've been successful so far but this one has me stomped.
> > I'm going to use a metaphor to describe it: let's say I have birds and
> > cages. I can create a bunch of cages, and for each cage I can create a
> > bunch of birds. In JS it will look something like this:
> >
> > cage1 = new Cage(...)
> > cage2 = new Cage(...)
> > bird1 = new cage1.Bird(...)
> > bird2 = new cage1.Bird(...)
> > bird3 = new cage2.Bird(...)
> >
> > For each cage there will be a constructor that will only create birds
> > that are in that cage. To make it more concrete: the bird will have
> > access to some variables of its cage. The reason why I like to do it
> > like this is because it will ensure that for each bird there will be
> > at least and no more that one cage; a paradigm I feel is natural to
> > the back-end I'm writing.
> >
> > In JS I would write something like this:
> > CageBird = function(cage, x){ this.cage = cage; this.x = x }
> > CageBird.prototype. (...)
> >
> > Cage = function(x) {
> >     this.x = x; var cage = this;
> >     this.Bird = function(x){ CageBird.call(this, cage, x) }}
> >
> > Cage.prototype. (...)
> >
> > Now how do I do this with the C++ API?
> > I can figure out I'll need to make two object templates, but I'm not
> > sure how to do the closure part as in the JS version I wrote above.
> > Your help is much appreciated!
> >
> > - Ferry
>
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
>

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to