=> Example Of Fat Arrow Format In Typescript Angular Code
FAT ARROW FUNCTIONS =>
For a long time, while learning about Angular 2, ionic 2, Typescript and ECMAScript 2015 (ES6), i had difficulty grasping the concept of fat arrow functions.
i found this tutorial on youtube which helped me alot to learn about fat arrow functions, so this is just a simple functions in javascrip (ES5) and ECMAScript (ES6) on how to use fat arrows
this is the old way format:var getRegvalue() = function(){ return 10;}console.log(getRegvalue()); // 10
THIS IS THE NEW FAT ARROW FORMATconst getArrowvalue = () =>{ return 10;}console.log(getRegvalue()); // 10
THIS IS A MORE SHORT AND CONCISE WAY OF DOING THE EXACT SAME FUNCTION AS ABOVE IF THE FUNCTION HAS A SINGLE STATEMENT WITHIN THE BODY THEN YOU CAN REMOVE THE CURLY BRACESconst getArrowvalue = () =>{return 10;}
YOU CAN EVEN MAKE IT SHORTER LIKE THIS IF ONLY A SINGLE STATEMENT IS WITHIN THE BODYconst getArrowvalue = () => return 10;
YOU CAN EVEN MAKE IT MORE SHORTER BY REMOVING THE 'return'const getArrowvalue = () => 10;
PASSING ARGUEMTENS IN FAT ARRROW FUNCTIONS:const getArrowvalue= (m) => 10*m; // m is a multiplierconsole.log(getRegvalue(10)); // returns 100 (10*10)
NOTE: if you are passing a SINGLE argument, you can omit the parenthesesconst getArrowvalue= m => 10*m;console.log(getRegvalue(10)); // return 100 (10*10)
OK, what if you have multiple arguments, here is an example:const getArrowvalue= (m,bonus) => 10*m+bonus;console.log(getRegvalue(10,50)); // return 150 (10*10)
to show you that getArrowvalue is really a function, you can use typeof like this:console.log(typeOf getRegvalue); // function
IF YOU STILL NEED MORE HELP, CHECKOUT THIS VIDEO, IT WAS VERY HELPFUL FOR ME
ES6 and Typescript Tutorial - 10 - Arrow Functions
https://www.youtube.com/watch?v=4N-L3Mmzu0Y&list=PLC3y8-rFHvwhI0V5mE9Vu6Nm-nap8EcjV&index=7
FAT arrow functions are great because of lexicals. and also with ecmascript you can declare default values within a fuction,
just like php, you can setup default paramater in a fuction.
for example:let (getRegvalue(a=50){ return a*10;}