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

  • 相关阅读:
    学习DButils笔记
    ubuntu16.04 backup and restore
    Oracle 中 编写 function 和 procedure 的注意事项
    利用反射和JDBC元数据实现更加通用的查询方法
    Memcached
    HTML, CSS
    day35作业
    day34作业
    MySQL数据库
    并发编程
  • 原文地址:https://www.cnblogs.com/mafeifan/p/5156999.html
Copyright © 2011-2022 走看看