More on other functions, IIFE, Constructor Function, etc.

// Function Expression
f1();
f2 = function() {
    console.log('Function expression');
}

// Function Definition
function f1(){
    console.log('Hello');
}
/*Arrow function
Useful for precise coding
*/

const f = ()=>console.log('Default Arrow function');

// mutliline

const multi = () => {

    console.log('Line 1');
    console.log('Line 2');
};

multi();

// Default Parameter

function f3(name="Unknown") {
    console.log('Hi ',name);
}

f3();

Anonymous Function: 

Anonymous function is nameless function. It cannot be defined independently, as we cannot invoke nameless function. Either can be used with function expression or as a call back function.

/**Anonymous function */

setTimeout(function(){
    console.log('I am anonymous');
},10000);

IIFE - Immediately Invoked Function Expression 

(function(){
    console.log('This is IIFE');
})();

Arguments Object

JavaScript provides an option for receiving arguments using default keyword arguments. We can pass required argument by directly accessing this keyword as below.

/**arguments Object */

(function(){console.log(arguments)

    for (let arg in arguments)
        console.log(arguments[arg])
})(3,4,5,'Indore');

Contructor functions: 

Constructors in JavaScript are created directly for the functions unlike class objects in other programming languages. 

function Person(name,age){
    console.log(this);
}

Person('Hari',30);


Here , this represent global object in node and windows object in browser. 

Remember when JavaScript source is loaded GEC is the first program which gets loaded, it has lexical environment( variable members and reference) , this and metadata. So, this represent global object which is created while JavaScript is loaded. 

function Person(name,age){
    console.log(this);
}

 Person('Hari',30);
 global: [Circular *1],
  clearImmediate: [Function: clearImmediate],
  setImmediate: [Function: setImmediate] {
    [Symbol(nodejs.util.promisify.custom)]: [Getter]
  },
  clearInterval: [Function: clearInterval],
  clearTimeout: [Function: clearTimeout],
  setInterval: [Function: setInterval],
  setTimeout: [Function: setTimeout] {
    [Symbol(nodejs.util.promisify.custom)]: [Getter]
  },
  queueMicrotask: [Function: queueMicrotask],
  structuredClone: [Function: structuredClone],
  atob: [Function: atob],
  btoa: [Function: btoa],
  performance: [Getter/Setter],
  fetch: [Function: fetch],
  crypto: [Getter],
  navigator: [Getter]
}


This is global or window object, all the functions are part of same global/window object. Thus, even we are calling function and executing it, it is getting called via global.function(). Hence, JavaScript is Object Oriented.

Now, if we want we can create a independent constructor function using new keyword as per below.

function Person(name,age){
    this.name = name;
    this.age=age;
    console.log(this);
}

 const P1 = new Person('Hari',30);

Person { name: 'Hari', age: 30 }

PS C:\Users\hsapkota\OneDrive - Capgemini\Documents\Ineuron\JS> 

Generator function

In python, we had special function called generator function which yield output and Pause, and next method releases/ process the output.

Similarly, in JavaScript generator provides a way to yield various values and process them latter. While we create a generator function and call it. A generator object is returned which has generated elements that can be susequently accessed with next() method.

function* counter(){

    yield 1;
    yield 2;
    yield 3;
}
let x = counter();
console.log(x)

console.log(x.next())

# this returns 

{ value: 1, done: false }

function* counter(){

    yield 1;
    yield 2;
    yield 3;
}
const x = counter();
let obj;
obj = x.next();
   
while(!obj.done)
{
    console.log(obj.value);
    obj = x.next();    
}

await and sync - to be read further ** 

Comments