Discuss / JavaScript / 箭头函数练习

箭头函数练习

Topic source

吉岸齐昂

#1 Created at ... [Delete] [Delete and Lock User]

arr.sort((x, y) => x -y); console.log(arr); // [1, 2, 10, 20]

Leepyq

#2 Created at ... [Delete] [Delete and Lock User]

为什么这样子写,就能排序了?

栗子墙

#3 Created at ... [Delete] [Delete and Lock User]

arr.sort((x, y) => x > y);

栗子墙

#4 Created at ... [Delete] [Delete and Lock User]

arr.sort((x, y) => x > y);

Code_Jack

#5 Created at ... [Delete] [Delete and Lock User]

你之前看到的是

if (x < y) {
    return -1;
}
if (x > y) {
    return 1;
}
return 0;

在MDN手冊的記載 是提到篩選的方式是 返回正數跟返回負數 <img src="https://i.imgur.com/m2Ruq7C.jpg">

所以這個時候這樣寫 返回x-y 不是正數就是負數或者是0 很簡易的寫法

Code_Jack

#6 Created at ... [Delete] [Delete and Lock User]

@廖老師 這個評論區的文字編輯器是不是有BUG? 預覽效果跟實際的發表效果不一樣啊QQ


  • 1

Reply