Hey! @ Beast, your NG API dropped--angular.identity and Angular.noop

Source: Internet
Author: User

@ The beast's NG API learning--angular.identity and Angular.noop

Angular.identity

The function returns the first parameter of itself. This function is generally used for function style.

Format: angular.identity ()

Using code:

<script>

Angular.module ("Demo", [])

. Controller ("Testctrl", ["$scope", function ($scope) {

var getresult = function (FN, Val) {

Return (fn | | angular.identity) (val);

};

var result = GetResult (function (n) {return n * 2;}, 3); result = 6

var null_result = GetResult (null, 3);//Null_result = 3

var undefined_result = GetResult (undefined, 3);//Undefined _result = 3

}])

</script>

Angular.noop

An empty function that does not perform any action. This function is generally used for function style.

Format: Angular.noop ();

Post code:

<script>

Angular.module ("Demo", [])

. Controller ("Testctrl", ["$scope", function ($scope) {

var _console = function (v) {

Return v * 2;

};

var getresult = function (FN, Val) {

Return (fn | | angular.noop) (val);

};

var firstresult = GetResult (_console, 3);//6

var secondresult = GetResult (null, 3);//undefined

var thirdresult = GetResult (undefined, 3);//undefined

}])

</script>

@ Beast all these 22 API understanding is also a bit drunk, in general, these two methods are used to write functions, according to the Code of the demo and running results, @ The beast thinks their role is to prevent the function from passing in null or undefined or other objects that cannot be manipulated. Because if you get rid of these two, when you pass the function call to the null/undefined/or other objects can not be executed, then the console is directly error ...

The two source code is also very simple, on 2-3 lines, and each line is not more than 20 letters.

Well, today write 2 @ Beast API Learning to understand it, recently have to learn to hurry and write faster, or else can not write those better usage or something ....  Today, my little friend asked me questions and asked me to write as soon as possible ... But time is limited. Pro t_t

Hey! @ Beast, your NG API dropped--angular.identity and Angular.noop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.