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

前端 未结 4 1774
一整个雨季
一整个雨季 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:12

    Lets say we have these two below functions:

    $scope.square = function(n) {
    return n * n
    };
    
    
    $scope.multplybyTwo = function(n) {
    return n * 2
    };
    

    To call this in a functional way:

    $scope.givemeResult = function(fn, val) {
    return (fn || angular.identity)(val);
    };
    

    Then You may use the above function something like below:

    $scope.initVal = 5;
    $scope.squareResult = $scope.givemeResult($scope.square, $scope.initVal);
    $scope.intoTwo = $scope.givemeResult($scope.multplybyTwo, $scope.initVal);
    

    You may follow the below link:

    http://litutech.blogspot.in/2014/02/angularidentity-example.html

提交回复
热议问题