排序()后为什么A和B相等?

exe*_*ook 1 javascript sorting

function randOrd() {
    return (Math.round(Math.random()) - 0.5)
}

A = [0,1,2,3,4,5,6,7]
var B = A.sort(randOrd)
console.log('A=',A)
console.log('B=',B)
Run Code Online (Sandbox Code Playgroud)

输出:

a= [ 3, 4, 0, 1, 6, 2, 5, 7 ]
b= [ 3, 4, 0, 1, 6, 2, 5, 7 ]
Run Code Online (Sandbox Code Playgroud)

我希望a是原始数组并b进行排序.但它们都是平等的(排序的),为什么?

chs*_*hsh 5

因为Array.sort()方法就地排序然后返回数组.

  • 几秒钟打败我.Upvoted. (2认同)