Functions Flashcards

1
Q

What is default parameters?

A
const func1 = function(name, birthYear,= 1986, age= 35)
{

}

If we dont specify the parameter value the default values will be considered.

How to skip one parameter?

func1(bharath, undefined, 5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain pass-by-value and pass-by-reference?

A

In JS when we pass primitive data types to the function, the value is copied . this is called pass-by-value.

in pass-by-reference, we pass the address and not the value is not coped.

In JS we dont have pass-by-reference, we have only pass-by-value.

let name = "bharath";
let person = {
   name: "bharath",
   country: "India"
}
const func1 = function( nameP, personP)
{
   nameP = "newname";
   personP.name = "newName"
}

func1(name,person);

console. log(name);
console. log(person)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are first class functions?

A

In JS, functions are treated as values. This is called first-class-functions.

a. Functions are created as values.
b. They are passed as arguments to another function. Example, addEventListeners.
c. They can returned as a value.
d. We can call methods on the function. apply bind

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a higher order function?

A

A function that recieves/or returns another function as a parameter is called higher order function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is call and apply methods?

A

call and apply methods allow the function which belongs to one object to be called using another object.

const airCanada = {
   name: "Air Canada",
   book(passengerName){
   console.log(`tickets booked on ${this.name}`);  
   } 
}
const airIndia = {
   name = "Air India"
}

book = airCanada.book;

book.call( airIndia, “bharath”);

apply is same but we have to pass array as arguments.,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a bind method?

A

bind method allow the function which belongs to one object to be called using another object.

const airCanada = {
   name: "Air Canada",
   book(passengerName){
   console.log(`tickets booked on ${this.name}`);  
   } 
}
const airIndia = {
   name = "Air India"
}

book = airCanada.book;

bookAirIndia = book.bind(airIndia);

bookAirIndia(‘bharath’);

We can also provide partial arguments.
bookAirIndia = book.bind(airIndia, ‘bharath’);

bookAirIndia()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a closure?

A

A function will always have access to the variable environment of the execution context in which it was created even after the execution context is gone.

example, create a function count and it has a variable count

counter method will declare one more function increment. Increment function increments the count.

const counter = function(){
let count;
   increment(){
      count++;
   }
}

According to the scope chain, increment method will have access to the count variable.

count counterFunc = counter();

counterFunc();
counterFunc()
counterFunc();

now the count is three. How is this possible because count is removed form the execution context.

This is possible because the function will have the variable context of environment context in which it was created.

In inspect, we can check the scope and there will be a closure.

Closures are very usefull in callback functions such as setTimeOut. We create a callback function inside a function. The callback function will have a closure and hence it can still access the variables where the function was created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly