zoukankan      html  css  js  c++  java
  • angular.identity

    解释:http://stackoverflow.com/questions/15421502/is-there-any-good-example-of-use-cases-for-angular-identity

    看ng文档有个奇怪的方法angular.identity

    好像就是你传什么就返回什么。

    官方文档给的例子是:

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

    实际上等价于

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

    运用场景

    // 有下面方法,返回一个数的平方
    $scope.square = function(n) {
       return n * n
    };
    
    
    //把这个方法作为参数使用
    
    $scope.givemeResult = function(fn, val) {
       return (fn || angular.identity)(val);
    };
    
    $scope.initVal = 5;
    $scope.squareResult = $scope.givemeResult($scope.square, $scope.initVal);

    返回25,如果

    $scope.squareResult = $scope.givemeResult($scope.square2, $scope.initVal);

    因为$scope.square2根本不存在,所以返回5

  • 相关阅读:
    1023. 组个最小数
    1021. 个位数统计
    *1020. 月饼
    *1019. 数字黑洞
    1016. 部分A+B
    *1014. 福尔摩斯的约会
    *1013. 数素数
    *1012. 数字分类
    1011. A+B和C
    *1008. 数组元素循环右移问题
  • 原文地址:https://www.cnblogs.com/mafeifan/p/5156999.html
Copyright © 2011-2022 走看看