Lesson 9: Functions: Continued
Shorthand functions (arrow functions)
Arrow Functions can get even shorter!
If there is only one statement and the statement return a value, the brackets and return keyword can be removed.
Example:
Greeting = () => "Hello World";
Hello World
Arrow Function with a parameter
Rule -Parameters are all defined in the left of the arrow function in the parenthesis.
myAge =(age) => "My age is" + age;
Calling an arrow function with a parameter without parenthesis
myAge = age => "My age is" + age;
Examples of built in functions in most IDEs
The most popular built in function is the constructor. What is a constructor?
- Returns the function that is created by the objects instances.
Example:
function meal (drink,chips,burger,toy){
this.myDrink = drink;
this.myChips = chips;
this.myBurger = burger ;
this.myToy = toy;
var myMeal = new meal("Fanta" , "Regular Chips" , "Hamburger" , "Hulk Toy");
var yourMeal = new meal("Coke" , "Small Chips" , "Chicken Burger" , "Spider man Toy");
}
Further Reading