function declaration
types of function
by manoj_antony32
JavaScript
//function declaration, hoisting happen for fun decl
function sample() { //named function
/* console.dir(sample)
console.log(sample.bind(this)) */
}
sample();
// function expression = anonymous function(function dont have name)
// hoisting won't happen for anonymous function, only variable declared not defined
const exp = function () {
return sample();
}
exp();
//IIFE
(function(name){
//console.log(name)
})('manoj antony');
//arrow function this concept
function test() {
this.name = "manoj divya";
const self = this;
this.display = function() {
console.log(this.name); // will not work
console.log(self.name);
}
this.display_arrow = () => {
console.log(this.name); // arrow function will handle this
}
}
const obj = new test();
const obj1 = obj.display;
const obj2 = obj.display_arrow;
obj1()
console.log('****end****');
/* obj1();
obj2(); */
//object function call and dynamic function name call
const y = 'mano';
const objt = {
x() { // x: function()
console.log('test x')
},
[y]() { // dynamic function name call
console.log('dynamic function');
}
};
objt.x();
objt.mano();
//Another type of function declaration, this wont recomment to use
// closures wont work, everthing works in global scope
const addr = new Function('a','b','c','return a+b');
//console.log(addr(3,5));
//generators
function* generat() {
yield 1;
console.log('next');
yield 2;
}
const gen1 = generat();
console.log(gen1.next());
console.log('before');
console.log(gen1.next());
console.log(gen1.next());
// function test() {
// this.name = "manoj divya";
// const self = this;
// this.display = function() {
// //console.log(this.name);
// console.log(self.name);
// }
// this.display_arrow = () => {
// this.anotherName = 'Hello';
// console.log(this.anotherName); // arrow function will handle this
// }
// }
// const obj = new test();
// obj.display();
// obj.display_arrow();
/* const obj1 = obj.display;
const...