0

Is there a way I can do this? In my code snippet below, this doesn't have access to sortByName FN when called from within sortByLyncStatus

prototype defn:

sortByName: function (bud, light) {
  if (bud.FullName < light.FullName)
    return -1;
  if (bud.FullName > light.FullName)
    return 1;
  return 0;
},

sortByLyncStatus: function (chuck, norris) {
  if (chuck.LyncAvailability == "Available" && norris.LyncAvailability == "Available") {
    return this.sortByName(chuck, norris);
  }
  else {
    if (chuck.LyncAvailability == "Available")
      return -1;
    if (norris.LyncAvailability == "Available")
      return 1;
    else { // both are away, inactive, offline, or out of office
      var chuckLastActive = $.toDateFromJson(chuck.LyncLastActive);
      var norrisLastActive = $.toDateFromJson(norris.LyncLastActive);
      if (chuckLastActive < norrisLastActive)
        return 1;
      if (chuckLastActive > norrisLastActive)
        return -1;

      return this.sortByName(chuck, norris);
    }
  }
}

solution

some.prototype.obj = {

  sortByName: function (a, b) {
    // omitted for solution
  },

  sortByLyncStatus: function(a, b) {
    // compare cases

    // default to name comparison
    return some.prototype.obj.sortByName(a, b);
  }
}
Joe
  • 797
  • 1
  • 10
  • 23
  • 4
    Please stop with these variable names. Please. – Evan Davis Aug 18 '14 at 14:40
  • 1
    You can call the function, but you'll have to arrange for there to be a reference to it by some other means. The `.sort()` method doesn't bind `this` in the way your code assumes. – Pointy Aug 18 '14 at 14:41
  • @Mathletics It's a sort function ... Maybe `function (angry, bird)` is better suited for your purposes? :) – Joe Aug 18 '14 at 14:42
  • @Pointy Such as a global var? `this` is a reference to the browsers Window object – Joe Aug 18 '14 at 14:47
  • 1
    Yes, a (relatively) global variable, so that the "sortByName" function is somehow visible to code in the other comparator. – Pointy Aug 18 '14 at 14:48
  • Those aren't sort functions, those are **compare** functions. Whose parameters are typically named `a` and `b`, btw. – Bergi Aug 18 '14 at 14:53
  • @Pointy Perfect, I'm able to access `sortByName` by preceding the call with the prototype name – Joe Aug 18 '14 at 14:57

1 Answers1

1

this will refer to the window for most sorts. Unless you define sortByName as a global function, it won't exist as a method on this.

You can define it as a function rather than a method and call it directly, like so:

var sortByName = function(a,b){};

var sortByLyncStatus = function(a,b){
    return sortByName(a,b);
}
ckersch
  • 7,507
  • 2
  • 37
  • 44
  • It looks like the OP's example actually consists of two methods in an object, so `this` should correspond to the object – Bojangles Aug 18 '14 at 14:48
  • @Bojangles Nope, @ckersch is correct that within the `sort`, `this` is a referring to the Window obj – Joe Aug 18 '14 at 14:50
  • If you use `array.sort(object.sortComparatorMethod)`, `this` still refers to the window. I think it's because of how it's called internally in the `sort` method on the array. – ckersch Aug 18 '14 at 14:52