javascript angular.identity() 是否有很好的用例示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15421502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:43:41  来源:igfitidea点击:

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

javascriptfunctional-programmingangularjs

提问by KailuoWang

According to the documentation

根据文档

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

I am wondering where I can find a good example of such use case - writing code in functional style in an angular app. Thanks

我想知道在哪里可以找到此类用例的一个很好的示例 - 在 angular 应用程序中以函数式风格编写代码。谢谢

回答by Stewie

Example from the AngularJS source code:

来自 AngularJS 源代码的示例:

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

Explanation:

解释:

In case transformationFnis provided as first parameter, it will be called with valueas it's own parameter. Otherwise identityfunction will be called with the same value.

如果transformationFn作为第一个参数提供,它将使用value它自己的参数调用。否则identity将使用相同的值调用函数。

As ng source code mentions:

正如 ng 源代码所提到的:

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.

这意味着在函数式编程中没有全局变量,所以你总是必须传递/注入你需要的一切。

回答by Asutosh

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

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

回答by Jason Swett

Correct me if I'm wrong, but my understanding is that this

如果我错了,请纠正我,但我的理解是

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

could be "un-refactored" to this

可以“未重构”到这个

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

which would be functionally equivalent to this identity-less version:

这在功能上等同于这个identity-less 版本:

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

So I guess the use case would be when you want to apply a certain transformation to a value when you're supplied with something that may or may not actually exist.

所以我猜这个用例是当你想对一个值应用某种转换时,当你提供了一些可能存在或可能不存在的东西时。

I wanted to better explain the identityfunction (as I understand it), although as I review my answer I don't think I really answered your question. Leaving my answer here anyway in case it's helpful.

我想更好地解释这个identity函数(据我所知),尽管在我回顾我的答案时,我认为我并没有真正回答你的问题。无论如何,将我的答案留在这里,以防万一。

回答by Paul Sweatte

angular.identityis useful for cases where you want to reference an object bound to a parent by passing it from a parent to its children.

angular.identity在您希望通过将对象从父级传递到其子级来引用绑定到父级的对象的情况下非常有用。

An identity function is like the zero for functions. Kind of useless by itself, but occasionally useful as part of an expression using higher-order functions, where you can take a function as a parameter or return it as a result.

恒等函数就像函数的零。本身有点无用,但偶尔作为使用高阶函数的表达式的一部分有用,您可以将函数作为参数或作为结果返回。

Here is an example for array values:

以下是数组值的示例:

For example, you may bind a two-dimensional array to an initial selection, and then bind the contained inner arrays to each subselection. The values function in this case is the identity function: it is invoked for each group of child elements, being passed the data bound to the parent element, and returns this array of data.

例如,您可以将二维数组绑定到初始选择,然后将包含的内部数组绑定到每个子选择。在这种情况下,values 函数是恒等函数:它为每组子元素调用,传递绑定到父元素的数据,并返回此数据数组。

Use it when it is necessary to pass a dummy function to:

当需要传递一个虚拟函数时使用它:

  • a promise ($q)
  • a compiler ($compile)
  • a unit test (spy/mock)
  • 一个承诺($q)
  • 编译器($compile)
  • 单元测试(间谍/模拟)

which acts as a data source or pump for a pipe|filter.

它充当数据源或管道的泵。

By comparison, Angular.noopincorrectly handles resolving/rejecting promises correctly because it always returns undefined, whereas Angular.identitycorrectly handles resolving/rejecting promises because it always returns the argument passed to it( f(x) = x).

相比之下,Angular.noop错误地正确处理解决/拒绝承诺,因为它总是返回未定义,而Angular.identity正确处理解决/拒绝承诺,因为它总是返回传递给它的参数(f(x) = x)。

In terms of constructors in Angular, it is relevant as such:

就 Angular 中的构造函数而言,它是相关的:

JavaScript engines only return the instance of a constructor if the constructor does not explicitly return an object (i.e. a value of the type object or function). Hence new identity(value) is always an object. Thus new identity(value) == value only returns true if value is an object. This is because the equality operators in JavaScript always check for identity when one of the operands is an object.

如果构造函数没有显式返回对象(即类型对象或函数的值),JavaScript 引擎只会返回构造函数的实例。因此 new identity(value) 始终是一个对象。因此,如果 value 是一个对象,则 new identity(value) == value 仅返回 true。这是因为当操作数之一是对象时,JavaScript 中的相等运算符总是检查身份。

References

参考