Sunday, 15 April 2012

javascript - If Array.prototype.slice() method returns shallow copy why does changing the value of sliced array does not change the original -



javascript - If Array.prototype.slice() method returns shallow copy why does changing the value of sliced array does not change the original -

the array.prototype.slice() method documentation says

the slice() method returns shallow re-create of portion of array new array object.

if case why modifying array returned array.prototype.slice() not alter original value.

> = [1, 2, 3] [ 1, 2, 3 ] > b = a.slice(0) [ 1, 2, 3 ] > b[0] = 4 4 > b [ 4, 2, 3 ] > // expected output should same output of b if b shallowcopy of a. [ 1, 2, 3 ]

may re-create on write happening while trying modify array a. if technically right array.prototype.slice() homecoming shallow copy.

the result of a.slice new array, it's not pointer initial variable.

javascript

No comments:

Post a Comment