On Oct 18, 6:01 am, buda <[email protected]> wrote:
> here the code http://jsfiddle.net/QW8vM/10/

`Object.clone` returns a raw object with a shallow copy of the
properties of the object you give it. It is not a perfect copy of the
object down to the prototype level:

var a, b;
a = [1, 2, 3];
b = Object.clone(a);
display("Object.isArray(a): " + Object.isArray(a)); // "true"
display("Object.isArray(b): " + Object.isArray(b)); // "false" <===

If you want to copy an array, use Array#slice (but note that it only
copies numeric properties, so if you've added non-numeric properties
to the array, you'll have to handle them):

var a, b;
a = [1, 2, 3];
b = a.slice(0); // Copy the array
display("Object.isArray(a): " + Object.isArray(a)); // "true"
display("a = " + a.join(", "));                     // "1, 2, 3"
display("Object.isArray(b): " + Object.isArray(b)); // "true"
display("b = " + b.join(", "));                     // "1, 2, 3"

Here's a working version of your fiddle:
http://jsfiddle.net/QW8vM/11/

Somewhat off-topic, but I find that `a` constructor very odd. `add`
always adds to a central array shared by all instances created by the
`a` constructor, but *every access* to the `items` property *copies*
that central array. Making a copy of an array on property access is
very surprising behavior, that's normally the sort of thing a function
is for. Consider:

var foo = new a();
var x = foo.items;
var y = foo.items;

Anyone reading that could would expect that `x` and `y` referred to
the same array, but of course they don't in the case of your `a`
constructor. Very surprising. Similarly:

var one = new a();
var two = new a();
one.add("foo");
one.add("bar");
alert(two.items.length); // "2"?!?! I haven't added anything to `two`!

I'm curious what the use-case is...

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

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