简体   繁体   中英

Why function does not change the array?

Consider below JavaScript code:

function myArr(arr){
    arr = arr + arr;
    return arr;
}

var arr = new Array(1,3,2,5);
myArr(arr);
document.write(arr); // Output : 1,3,2,5
arr = arr + arr;
document.write(arr); // Output : 1,3,2,51,2,5

Why function myArr() is returning the same array while we are performing the same operation inside and outside the function?
Why two different behaviors are showing here with the same operation statement?

Because you aren't assigning the value returned by myArr() method to the variable. Its should be arr = myArr(arr); rather than myArr(arr); try following,

function myArr(arr){
    arr = arr+arr;
    return arr;
}
var arr = new Array(1,3,2,5);
arr = myArr(arr);
document.write(arr);// It should give Output : 1,3,2,5,1,3,2,5

var arr = new Array(1,3,2,5);
arr = arr+arr;
document.write(arr);// Output : 1,3,2,5,1,3,2,5

Your function returns the modified array. You called the function myArr(arr) but didnot store the result that your function returned. It should be like

    function myArr(arr){
    arr = arr+arr;
    return arr;
}
var arr = new Array(1,3,2,5);
arr=myArr(arr);

There is no pass-by-reference in javascript.

Inside the function myArr , arr has a nested scope so it never took the arr value from the global scope.

 function myArr(arr){ arr = arr+arr; // creates new arr at a different location return arr; } var arr = new Array(1,3,2,5); arr = myArr(arr); document.write(arr); 

 function myArr(arr){ console.log(typeof(arr)); // pass as a Object arr = arr+arr; // + operator do concatination string console.log(typeof(arr)); // now converted in String return arr; } var arr = new Array(1,3,2,5); myArr(arr); document.write("Output 1="+arr+"<br/>");// Output : 1,3,2,5 arr = arr+arr; // + operator do concatination string // 1,3,2,5 + 1,3,2,5 ==> type String( 1,3,2,51,3,2,5 ) document.write("Output 2="+arr);// Output : 1,3,2,51,3,2,5 

code below is the same as code you give,but it is more easy to understand:

    function myArr(array){
        array = array + array;
        return array;
    }

    var arr = new Array(1,3,2,5);
    myArr(arr);
    document.write(arr); // Output : 1,3,2,5
    arr = arr + arr;
    document.write(arr); // Output : 1,3,2,51,2,5

Look out!!! arr is the global variable while array is the partial variable.

If you change array in myArr to arr ,that doesn't matter.

Ppartial variable only exists when the function is running except closure.

Explanation is in the comment:

    var arr = new Array(1,3,2,5);//arr=[1,3,2,5];

    myArr(arr);
    function myArr(array){//array=[1,3,2,5],array===arr;
        array = array + array;//array=1,3,2,51,3,2,5;arr didn't change!!!
        return array;//return 1,3,2,51,3,2,5;arr still didn't change!!!
    }

    document.write(arr);//arr=[1,3,2,5],so you see 1,3,2,5

    arr = arr + arr;//arr=1,3,2,51,3,2,5

    document.write(arr);//so you see 1,3,2,51,3,2,51,3,2,5 because it didn't erase the last content.In this case,document write 1,3,2,51,3,2,5 after 1,3,2,5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM