javascript - AngularJS - valueFn - Stack Overflow

What's the point of functionfunction valueFn(value) {return function() {return value;};}defined in

What's the point of function

function valueFn(value) {return function() {return value;};}

defined in angular.js.

It's used for ex. in

var lowercaseFilter = valueFn(lowercase);
register('lowercase', lowercaseFilter);

What is different if we used lowercase directly as in

register('lowercase', lowercase);

instead of the previous line.

The same way, in the method

function ngDirective(directive) {
  if (isFunction(directive)) {
    directive = {
      link: directive
    }
  }
  directive.restrict = directive.restrict || 'AC';
  return valueFn(directive);
}

why the last line is not

return directive;

but

return valueFn(directive);

What's the point of function

function valueFn(value) {return function() {return value;};}

defined in angular.js.

It's used for ex. in

var lowercaseFilter = valueFn(lowercase);
register('lowercase', lowercaseFilter);

What is different if we used lowercase directly as in

register('lowercase', lowercase);

instead of the previous line.

The same way, in the method

function ngDirective(directive) {
  if (isFunction(directive)) {
    directive = {
      link: directive
    }
  }
  directive.restrict = directive.restrict || 'AC';
  return valueFn(directive);
}

why the last line is not

return directive;

but

return valueFn(directive);
Share Improve this question asked Feb 23, 2013 at 13:01 DraganSDraganS 2,7091 gold badge29 silver badges43 bronze badges 1
  • 2 It is because functions like register required function to be passed as the second argument, and the function has to return the configuration object. the valueFn returns a function which returns the configuration object. – Arun P Johny Commented Feb 23, 2013 at 13:10
Add a ment  | 

1 Answer 1

Reset to default 12

Filters (and some other services) are regular functions, but AngularJS heavily relies on Dependency Injections so those filters (functions) are not exposed to the clients as-is (as instantiated objects), but instead are created by factory functions. This is done so AngularJS DI system can inject dependencies to a factory function (and those dependencies are available in a closure later on).

But there are cases where there is nothing to inject and the whole factory function job boils down to returning a pre-instantiated value. This is the case for the lowercaseFilter (and many other filters). So instead of repeating the same, almost-do-nothing factory function, the valueFn was created to avoid code repetition. Without it AngularJS code would have to contain:

var lowercaseFilter = function(){
  return lowercase;
};
var uppercaseFilter = function(){
  return uppercase;
};
...

It is just more concise to write:

var lowercaseFilter = valueFn(lowercase);
var uppercaseFilter = valueFn(uppercase);
...

In short - I suspect that the reason is DRY.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742420174a4440526.html

相关推荐

  • javascript - AngularJS - valueFn - Stack Overflow

    What's the point of functionfunction valueFn(value) {return function() {return value;};}defined in

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信