Functions are loosely typed in javascript, we are able to pass different types of parameters, too few args, too many argos etc. Keep function simple and make do only 1 thing which might be not feasible in real life. Function can have parameters or default values. Functions can return a value or not.
Function expression : this type of function can be called only after being defined, generally preferred
const areaBuilder= function () { //function expression
console.log(‘area builder’)
};
areaBuilder(); // call the above function
const areaBuilder= function (h,w) { //function expression
console.log(w);
console.log(h);
console.log(‘area builder’ + (w*h));
};
areaBuilder(2,10);
const areaBuilder= function (h,w) { //function expression
return (w*h); // return the value
};
const area = areaBuilder(2,10);
console.log(area);
Recursion : a function calling itself
const adder = function(num1,num2){
return num1+num2;
}
adder(2,’9’); // 29
adder(2,9) // 11
const adder = function(num1,num2){ // maybe this works?
return Number(num1)+Number(num2)
}
const areaBuilder= function (h,w= 9) { //function expression with default value
return (w*h); // return the value
};
areaBuilder(3); // 27
Probably nothing will happen if you pass more arguments than needed
Function Declaration : this function can be called before being defined
console.log(minuser(3,4));
function minuser (num1,num2){ // this can be called before getting defined
return num1-num2;
}
Hoisting : order the javascript reads the file, which is top to bottom
Arrow functions shorthand in ES6, have to be careful with ‘this’ object
Example
const areaBuilder = (h,w= 9)=> { //function expression with default value
return (w*h); // return the value
};
Why Undefined coming in chrome console?
the value of expression which is void/undefined is returned
Variable scope : to avoid unintended consequences, use const and let, do not use var
a var can only be called in current code block, function has access to outer item/variable
IIFE ->immediately invoked function expressions. An IIFE is a function that is defined and immediately executed. The defining characteristic of an IIFE is that the function is invoked immediately after it is created.
Arguments : variable is an array like but not an array. itwill create an array inside the function and use it with for each.
const arr = [1,2,4,3]
arr.forEach(num=>{
console.log(num);
})
Rest parameters : … to spread them out in an array-like structure, is it like “the ellipsis (...)” in c??
const myFunc = (...args) => {
// this will take arguments from a function call and spread it to an array
return args;
}
const myFunc = (fristName,...args) => {
// this will take arguments from a function call and spread it to an array
console.log(firstName);
return args;
}
console.log(myFunc(4,6,8));
Methods inside the objects
const car = {
make:”toyota”,
model : “Rav4”,
drive:function(){
console.log(‘VRoom’);
}
getSpecs(){
console.log(`Make:${this.make}`) // toyota
}
}
console.log(Car);
car.drive();
‘This’ object : keyword refers to the object that is currently executing the code. Its value depends on the context in which it is used.
Call :
You can use call() to "borrow" methods from one object and use them on another.
call() gives you explicit control over the ‘this’ keyword, which is crucial in object-oriented JS.
Const teacher = {
Says:”hello”,
Talk : function(punct){
console.log(this.says + punct);
}
}
Const student = {
Says: ‘wait’
}
teacher.talk.call(student, “!!!!”); // wait !!!!
Apply:
Key Differences between apply() and call():
call(): Takes individual arguments, separated by commas.
apply(): Takes an array of arguments.
When to use apply():
When you have an array of arguments that you want to pass to a function.
When you need to dynamically set the this value of a function.
For method borrowing (using a method from one object on another object).
Bind:
Maintaining context:
When passing object methods as callbacks, this can often be lost, leading to unexpected behavior. bind() helps you ensure that the correct this value is used.
Partial application:
bind() allows you to create new functions with some arguments already set, which can be helpful for creating reusable functions.
Classes : template for an object
Class Hero {
constructor (name, ability){
this.name = name;
this.ability = ability;
}
useAbility(){
Const {name,ability} = this;
}
}
Anonymous functions are functions that are not bound to an identifier (i.e., they do not have a name). They are often used as arguments to other functions or as immediately invoked function expressions (IIFE).
In your example:
(args) => {
// do something
}
This is an anonymous function (also known as an arrow function) that takes a single parameter args and performs some operations within its body.
Here's a more complete example to illustrate its usage:
const myFunction = (args) => {
console.log(args);
};
myFunction("Hello, world!"); // Outputs: Hello, world!
In this case, myFunction is assigned an anonymous arrow function that logs the args parameter to the console. While the function itself is anonymous, it is assigned to the variable myFunction, which can then be used to call it.