【问题标题】:eloquent javascript exercise 4.2 reversing an Array雄辩的 javascript 练习 4.2 反转数组
【发布时间】:2014-12-11 21:33:02
【问题描述】:

在下面的代码中,我不明白为什么 reverseArrayOnereverseArrayTwo 相比不返回反转数组。本质上,我相信在这两种情况下我都将 reversedArray 分配给 Array 。链接到问题http://eloquentjavascript.net/04_data.html#c_F3JsLaIs+m

function reverseArray(array) {
reversedArray = [];
  for (i=array.length-1; i>=0; i--) {
    reversedArray.push(array[i]);
  }
  return reversedArray;
}

function reverseArrayOne(array) {
  array = reverseArray(array);
  return array;
}

function reverseArrayTwo(array) {
  reversedArray = reverseArray (array);
  for (i=0; i<reversedArray.length; i++) {
    array[i] = reversedArray[i];
  }
  return array;
}
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayOne(arrayValue);
console.log(arrayValue);
// → [1,2,3,4,5]
reverseArrayTwo(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]

【问题讨论】:

标签: javascript arrays arguments global-variables scoping


【解决方案1】:

请记住,objectvalues,variables and literals 值的处理方式之间的区别。我添加了解释程序的 cmets,如果您需要更多详细信息,请提供答案。

主要区别是: 而对象是通过引用传递的,后者是通过值传递的

function reverseArray(array) {
reversedArray = [];
  for (i=array.length-1; i>=0; i--) {
    reversedArray.push(array[i]);
  }
//return a new array with elements in a reverse order of original array
  return reversedArray;
}

function reverseArrayOne(array) {
//scope of object reference is in the function only and hence is not reflected outside
  array = reverseArray(array);
//return the same object pointer which now points to a new array
  return array;
}

function reverseArrayTwo(array) {
      reversedArray = reverseArray (array);
      for (i=0; i<reversedArray.length; i++) {
//you are changing the properties of the same object(this is different from merely changing the reference of the object)
        array[i] = reversedArray[i];
      }
      return array;
    }
    var arrayValue = [1, 2, 3, 4, 5];
//passing arrayValue as reference
    reverseArrayOne(arrayValue);
//original arrayValue still points to the original array since the reverse value is not in scope.
    console.log(arrayValue);
    // → [1,2,3,4,5]
    reverseArrayTwo(arrayValue);
//the original array is the same,but the internal property of the object has been changed and hence is reflected
    console.log(arrayValue);
    // → [5, 4, 3, 2, 1]

【讨论】:

    【解决方案2】:

    当你将数组传递给函数时,函数可能会改变数组本身的值。

    reverseArrayOne(arrayValue) 返回一个反转数组,但不改变arrayValue 的值。

    reverseArrayTwo(arrayValue) 实际上会因为函数中的这一行而改变arrayValue的值:

    array[i] = reversedArray[i];
    

    在您的日志中,您显示的是 arrayValue 的值,而不是函数的返回结果

    console.log(arrayValue)
    

    第一个函数不会改变数组的值,所以它会返回 [1,2,3,4,5]。第二个函数实际上更改了数组值,因此它将返回 [5,4,3,2,1]。如果您只需要打印数组的反转,那么您应该这样做

    result = reverseArrayOne(arrayValue);
    console.log(result)
    

    【讨论】:

    • 为什么array = reverseArray(array)不改变arrayvalue,而array[i] = reversedArray[i]却可以?
    • @user3812377:第一个改变了array变量,而第二个改变了array引用的数组object
    【解决方案3】:

    我认为这可能是一个很好的方法......

    var list = ["orange", "apple", "pear"]
    var newList = [];
    
    function newReverse(list){
      for(var i=0; i<list.length; i++){
        newList.push(list[i]);
      }
      newList.reverse();
      return newList;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 2017-03-19
      • 1970-01-01
      相关资源
      最近更新 更多