Daud
Daud

Reputation: 7887

Which javascript construct is this AngularJS filter expression?

If we wish to use a filter in AngularJS on the controller side, we use this

$filter('filter')(array, expression, comparator, anyPropertyKey)

I can't understand which javascript construct is this using. I mean, its not a function, its not an assignment expression, its not a self-executing function. What is it ?

Upvotes: 0

Views: 130

Answers (2)

Zakaria
Zakaria

Reputation: 15070

It's a function that returns a function. Here is its source code :

function filterFilter() {
  return function(array, expression, comparator, anyPropertyKey) {
    if (!isArrayLike(array)) {
      if (array == null) {
        return array;
      } else {
        throw minErr('filter')('notarray', 'Expected array but received: {0}', array);
      }
    }

    anyPropertyKey = anyPropertyKey || '$';
    var expressionType = getTypeForFilter(expression);
    var predicateFn;
    var matchAgainstAnyProp;

    switch (expressionType) {
      case 'function':
        predicateFn = expression;
        break;
      case 'boolean':
      case 'null':
      case 'number':
      case 'string':
        matchAgainstAnyProp = true;
        //jshint -W086
      case 'object':
        //jshint +W086
        predicateFn = createPredicateFn(expression, comparator, anyPropertyKey, matchAgainstAnyProp);
        break;
      default:
        return array;
    }

    return Array.prototype.filter.call(array, predicateFn);
  };
}

Upvotes: 1

deceze
deceze

Reputation: 522155

It's a function which returns a function which you then execute.

function foo() {
    return function bar() {};
}

foo()();

// equivalent to:
var b = foo();
b();

Upvotes: 2

Related Questions