arrays - What does it mean for .slice() to be a "shallow clone"? -


actionscript's array , vector classes both have slice() method. if don't pass parameters, new array or vector duplicate (shallow clone) of original vector.

what mean "shallow clone"? specifically, difference between

array newarray = oldarray.slice(); vector.<foo> newvector = oldvector.slice(); 

and

array newarray = oldarray; vector.<foo> newvector = oldvector; 

? also, if vector's base type isn't foo, simple , immutable int?

update:

what result of following?

var one:vector.<string> = new vector.<string>()  one.push("something"); one.push("something else");  var two:vector.<string> = one.slice();  one.push("and thing");  two.push("and last thing");  trace(one); // something, else, , thing trace(two); // something, else, , last thing 

thanks! ♥

in context, .slice() make copy of vector, newarray refers different object oldarray, except both seem identical objects. likewise goes newvector , oldvector.

the second snippet:

array newarray = oldarray; vector.<foo> newvector = oldvector; 

actually makes newarray reference oldarray. means both variables refer same array. same newvector , oldvector — both end referring same vector. think of using rubber stamp stamp same seal twice on different pieces of paper: it's same seal, represented on 2 pieces of paper.

on side note, term shallow copy differs deep copy in shallow copy of only object while deep copy of object , properties.

also, if vector's base type isn't foo, simple , immutable int?

it's same, because variables refer vector objects , not ints.

what result of following?

your output correct:

 something, else, , thing something, else, , last thing 

two = one.slice(), without arguments, makes new copy of one current contents , assigns two. when push each third item one , two, you're appending distinct vector objects.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -