Is there any good example of use cases for angular.identity()?

前端 未结 4 1790
一整个雨季
一整个雨季 2021-02-19 10:03

According to the documentation

A function that returns its first argument. This function is useful when writing code in the functional style.

I

4条回答
  •  无人及你
    2021-02-19 10:09

    Example from the AngularJS source code:

    function transformer(transformationFn, value) {
      return (transformationFn || angular.identity)(value);
    };
    

    Explanation:

    In case transformationFn is provided as first parameter, it will be called with value as it's own parameter. Otherwise identity function will be called with the same value.

    As ng source code mentions:

    This function is useful when writing code in the functional style.

    What this means is that in functional programming there are no globals, so you always have to pass/inject in everything you need.

提交回复
热议问题