Functions Flashcards
Ho are functions defined in JS?
the function statement
example
function MyEasyFunction() {
console.log(‘My function was called’);
}//now call the function
myEasyFunction();
How can a function be defined as of E6?
Using arrow funtion syntax
const myEasyFunction = () => {
console.log(‘My arrow sytax’) ;
}
How can a parameter be defined for a function?
In parentheses like
myEasyFunction(param1){
or
const myEasyFunction = (param1) => {
How is data returned from a function?
Use the return command within the function. like
constr myEasyFunc = (par1,par2) => {
return par1 + par2;
}
//the call looks like
let mySum = myEasyFunc(100, 200);
/*this would return the value of mySum as 300 */
How can multiple parameters be defined for a function?
Separate the values with a comma like
function myEasyFunc(par1,par2) {
Describe a global closure?
It seems kind of like building an object
This involves creating a wrapper function to protect variables.
For example
const myPrice = (cost) => {
let myCost = cost; //wrapper variable
return (price = (markUp) => {
return (markUp += cost);
});
};
//use of the wrapper to build a
//new function with a set cost
const totPrice = myPrice(200);
let tenPerc = totPrice(20);
//would return 220 the main variable is protected