Hi,

The properties you pass into `Class.create` are put on the prototype
of the resulting constructor function ("class", for lack of a better
term). That means those properties are *shared* by all instances
created by that constructor function. So when either the `ChildA` or
`ChildB` constructor function writes to the properties on
`this.intro`, they're writing to properties on the *same object*. It's
shared between them. That's why you see the behavior you see.

To fix it, just change Parent to create a separate `intro` for each
instance; this is most easily done in the constructor. Separately,
your ChildA and ChildB need to call the superclass's constructor, and
there's a typo in your code in `ChildB` code ("introl" instead of
"intro"). Finally, any code using `document.write` will only work
during the main page load.

Corrected example:
http://jsbin.com/evada4

This all comes down to how prototypes sit behind instances. Consider:

var Foo = Class.create({
    bar: "I'm bar",
    show: function() {
        alert(this.bar);
    }
});
// Let's create an instance
var f = new Foo();
// `f` does *not* have a `bar` property, but `Foo.prototype` does
f.show(); // alerts "I'm bar"
alert(f.hasOwnProperty("bar")); // alerts "false"
// Now let's give `f` its *own* `bar` property
f.bar = "I'm the updated bar";
f.show(); // alerts "I'm the updated bar"
alert(f.hasOwnProperty("bar")); // alerts "true"
// Now let's remove `f`'s own `bar`
delete f.bar;
f.show(); // alerts "I'm bar" -- it's falling back to using the
prototype again
alert(f.hasOwnProperty("bar")); // alerts "false"

When you go to look up a property on an instance, if the instance
*itself* has that property, then that value is used. If not, we look
to the prototype and use its value if there is one. An instance gets
its *own* property if you assign to that property (`f.bar = ...;`
above). If you don't, it keeps using its prototype's property (if
any).

The thing with your `intro` property is that nothing is ever assigned
to it, and so no instance had its own `intro` property, and they were
all falling back on the one on `Parent.prototype`. All you ever did in
the `ChildA` and `ChildB` constructors was write to property *on* that
object; you never wrote to the `intro` property itself, and so you
never created a separate `intro` property for each instance.

HTH,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On Jan 12, 1:08 pm, 쏘주망태 <comah...@gmail.com> wrote:
> // my code :
>
> var Parent = Class.create({
>   intro:{
>      name:'',
>      age:0
>   },
>   initialize:function(){},
>   show:function(){
>     document.write(this.intro.name);
>   }
>
> });
>
> var ChildA = Class.create(Parent, {
>   initialize:function(){
>     this.intro.name = 'A';
>     this.intro.age = 10;
>   }
>
> });
>
> var ChildB = Class.create(Parent, {
>   initialize:function(){
>     this.intro.name = 'B';
>     this.introl.age = 20;
>   }
>
> });
>
> ex1)
> var a = new ChildA();
> var b = new ChildB();
>
> a.show(); // => print 'B'   <<<< I can't understand..:(
> b.show(); // => print 'B'
>
> ex2)
> var b = new ChildB();
> var a = new ChildA();
>
> a.show(); // print 'A'
> b.show(); // print 'A'  <<<< I can't understand..:(
>
> //=========================
> Why do these problems occur?

-- 
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-scriptacul...@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