Hi, That should work. I notice that you have "class.Create" rather than "Class.create"; I assume that's just an error in the note and that the actual code uses Class.create? Because if not, that's your problem.
> if(myInstance.isB())... > > => isB is undefined That indicates there's a different problem. isB should be defined regardless, you defined it in your class definition. If the Object.extend thing weren't working, it should *return* undefined, but not *be* undefined. FWIW, this works: * * * * (also at http://pastie.org/581050) // Defining var Thing = Class.create({ initialize: function(src) { if (src) { Object.extend(this, src); } }, isB: function() { return this.b; } }); // Using: var t; t = new Thing({"b": true}); alert("t.isB() ? " + t.isB()); * * * * HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Aug 11, 6:07 pm, Eric <[email protected]> wrote: > Hi, > > I make some Ajax calls to fetch objects from my server, and I would > like those object to be instance of some class. > I just cannot find a way to achieve this... > > Here is what I've tried so far : > > var MyClass = class.Create({ > initialize: function(src) { > if(src) Object.extend(this,src); > }, > isB: function() { return this.b;} > > }); > > var myJsonObject = {"a":0, "b":true}; // xrh.responseJSON > > myInstance = new MyClass(myJsonObject); > > if(myInstance.isB())... > > => isB is undefined > > I also tried the opposite approach with: > myInstance = Object.extend(myJsonObject,MyClass.prototype) > > if(myInstance.isB())... > => isB is undefined > > What is the correct way of achieving this ? > > Eric --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
