这种设计方式,在.net中叫委托,在java中叫监听。
sort方法的大致内部实现:
Array.prototype.sort=function(compareFunc) {
var temp;
for (var i=0;i<this.length-1;i++) {
for (var j=i+1;j<this.length;j++) {
if (compareFunc(this[i],this[j])>0) {
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
官方描述如下:
sort
Sorts the elements of an array.
方法源 Array
实现版本 Navigator 3.0, LiveWire 1.0
Navigator 4.0: modified behavior.
语法
sort(compareFunction)
参数
compareFunction Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically (in dictionary order) according to the string conversion of each element.
If compareFunction(a, b) is less than 0, sort b to a lower index than a.
If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.
If compareFunction(a, b) is greater than 0, sort b to a higher index than a.