重构匿名函数javascript

重构匿名函数javascript,javascript,knockout.js,Javascript,Knockout.js,我正在做击倒绑定。。我要执行淘汰排序,我已经这样做了 function notesViewModel() { _this.colName = "CreatedDate"; _this.sortOrder = "desc"; _this.notes = ko.observableArray(); _this.SortItems = function (colname) { var newNotes = _this.notes(); i

我正在做击倒绑定。。我要执行淘汰排序,我已经这样做了

function notesViewModel() {
    _this.colName = "CreatedDate";
    _this.sortOrder = "desc";
    _this.notes = ko.observableArray();

   _this.SortItems = function (colname) {
        var newNotes = _this.notes();
        if (_this.sortOrder === "desc") {
            this.notes(newNotes.sort(notesViewModel._getSortFunction = function (a, b) {
                return a[colname] < b[colname] ? -1 : 1;
            }));
            switchSortOrder();
        } else {
            this.notes(newNotes.sort(notesViewModel._getSortFunction = function (a, b) {
                return a[colname] > b[colname] ? -1 : 1;
            }));
            switchSortOrder();
        }
    };
    function switchSortOrder() {
        if (_this.sortOrder === "asc") {
            _this.sortOrder = "desc";
        } else {
            _this.sortOrder = "asc";
        }
    }
 ko.applyBindings(_this, $("body").get(0));
return _this;
}
函数notesViewModel(){
_this.colName=“CreatedDate”;
_this.sortOrder=“desc”;
_this.notes=ko.observearray();
_this.SortItems=函数(colname){
var newNotes=_this.notes();
如果(_this.sortOrder==“desc”){
this.notes(newNotes.sort(notesViewModel.\u getSortFunction=function(a,b){
返回a[colname]b[colname]?-1:1;
}));
切换器();
}
};
函数切换器(){
如果(_this.sortOrder==“asc”){
_this.sortOrder=“desc”;
}否则{
_this.sortOrder=“asc”;
}
}
ko.applyBindings(_this,$(“body”).get(0));
把这个还给你;
}
我的html代码如下所示:

<table id="notes" class="notes_table">
        <tr class="head">
        <th data-bind='click: function() { SortItems("CreatedDate")}'>
        <span>Date</span>
        </th>
        <th data-bind='click: function() { SortItems("Type")}'>
        <span>Type</span>
        </th>
        <th data-bind='click: function() { SortItems("Category")}'>
        <span>Category</span>
        </th>
        <th data-bind='click: function() {SortItems("AddedBy")}'>
        <span>Added by</span>
        </th>
        <th>
        <span>Alerts</span>
        </th>
        <th></th>
        </tr>
        <tbody data-bind="template: { name: 'StudentNote', foreach: notes }"></tbody>
    </table>

日期
类型
类别
由添加
警报
我想把sort函数重构成这样的东西

_

this.SortItems=函数(colname){
var newNotes=_this.notes();
this.notes(newNotes.sort(notesViewModel.\u getSortFunction=compareFunction(a,b,\u this.sortOrder));
函数比较器函数(a、b、排序器)
{
如果(排序器==“描述”)
{
返回a[colname]b[colname]?-1:1;
_this.sortOrder=“desc”;
}
}
};

但是它没有起作用,因为它说a和b参数不可用。。有人能告诉我如何将逻辑与匿名函数分离吗?

看起来您对什么是匿名函数有些困惑

(我认为)您试图做的是,基于“this”的字段“sortOrder”,使用一个函数对升序和降序进行排序

您的代码有几个问题:

  • 首先,调用函数而不是将其保存到this.sortOrder。这就是“a”和“b”不可用的原因,此函数只能由排序算法调用
  • 其次,“this”不会指向比较函数中的正确位置:要在Javascript中定义“this”对象,需要将函数存储在对象(或原型)上,或者使用特殊调用语义这在Javascript中很棘手,值得(重新)阅读一些教程以确保您理解它
  • 最后,在定义compareFunction之前调用它
这段代码符合您的要求,尽管我还没有测试它,也不知道您使用的框架。它在调用sort之前将sortOrder存储在外部函数中,以便内部函数可以使用它

this.SortItems = function (colname) {
  var sortOrder = this.sortOrder; // make sortOrder available to the sort function
  this.notes(_this.notes().sort(
    function (a, b) {
      return ((sortOrder === "desc") ? 1 : -1) * ((a[colname] < b[colname]) ? -1 : 1);
    }));
}; 
this.SortItems=函数(colname){
var sortOrder=this.sortOrder;//使sortOrder可用于排序函数
this.notes(_this.notes().sort(
功能(a、b){
返回((sortOrder==“desc”)?1:-1)*((a[colname]
Hi Stephen感谢您的回复。。我正在使用asp.net MVC4。。当我尝试使用你建议的代码时,它给了我“表达式statement不是赋值或调用”。在排序逻辑行上。
this.SortItems = function (colname) {
  var sortOrder = this.sortOrder; // make sortOrder available to the sort function
  this.notes(_this.notes().sort(
    function (a, b) {
      return ((sortOrder === "desc") ? 1 : -1) * ((a[colname] < b[colname]) ? -1 : 1);
    }));
};