IN JAVASCRIPT FUNCTIONS ARE OBJECT

FIRST CLASS FUNCTIONS In JAVASCRIPT FUNCTION are SPECIAL TYPES of OBJECTS which can have PRIMITIVES ,  NAME ( OPTIONAL )  ,  OBJECT ,  FUNCTIONS and your CODE.  Everything you can [...]

FIRST CLASS FUNCTIONS

  • In JAVASCRIPT FUNCTION are SPECIAL TYPES of OBJECTS which can have PRIMITIVES ,  NAME ( OPTIONAL )  ,  OBJECT ,  FUNCTIONS and your CODE
  • Everything you can do with OTHER TYPES such as STRINGS , NUMBERS , BOOLEANS , OBJECTS etc can be DONE with FUNCTIONS.
  • You can ASSIGN FUNCTION to VARIABLES 
  • FUNCTIONS can be PASSED as VALUE to ANOTHER FUNCTION
  • FUNCTIONS can be CREATED on the FLY like OBJECTS

function greet ( ) { 

   console.log ( ‘ hi ‘ );

   console.log ( greet.language);  

 }

greet.language = ‘ English’;

 // I added a PROPERTY to the greet function

greet ( ) ; // Invoked 

In the ABOVE PROGRAM function is a GLOBAL OBJECT  greet is its NAME and it has your code inside it. 

FUNCTION EXPRESSION 

  • A UNIT of CODE that RESULTS in a VALUE . 
  • It doesn’t have to be SAVED to a VARIABLE.
  • FUNCTION EXPRESSION are USUALLY REFERRED as ANONYMOUS FUNCTIONS.
  • The ANONYMOUS FUNCTION’S ADDRESS is ALWAYS STORE in an OBJECT
  • And in ORDER to INVOKE ANONYMOUS FUNCTION we need to POINT to the OBJECT and tell to RUN its CODE.

var a ;

a = 3 ;

1 + 2  // Its an EXPRESSION

 

// FUNCTION EXPRESSION 

var anonymousGreet = function () {

 console.log ( ” Welcome ” );

}

anonymousGreet is the OBJECT which keeps the ADDRESS of the ANONYMOUS FUNCTION ( EXPRESSION  )

anonymousGreet();

This will EXECUTE the ANONYMOUS FUNCTION or the FUNCTION EXPRESSION

 

FUNCTION STATEMENT 

  • FUNCTION STATEMENT is also CALLED as the FUNCTION DEFINITION
  • In the FUNCTION STATEMENT the FUNCTION has a NAME
  • And USING The FUNCTION NAME with PARENTHESIS the FUNCTION is INVOKED.

REFER To the CODE BELOW

greet ( ) ;  // INVOKING the greet Function

// Function Statement 

function greet ( ) {

 console.log ( ” Welcome ” ) ;

// Anonymous Function or Function Expression whose ADDRESS is saved in the OBJECT anonymousGreet

var anonymousGreet = function () {

 console.log ( ” Hello ” );

}  

anonymousGreet ( ) ; // INVOKE anonymous function 

// RECEIVING VARIABLE as OBJECT 

function log ( a ) {

a ( ) ;

// CALLING the log function and PASSING on the FLY a FUNCTION EXPRESSION  as OBJECT VARIABLE

log ( function () { console.log (‘hi’); } );  

  • We can PASS DATA to the FUNCTION in TWO WAYS ie PASS BY VALUE or BY REFERENCE 
  • ONLY PRIMITIVE DATA is PASSED BY VALUE where as OBJECT VARIABLES are PASSED BY REFERENCE.

If a is a PRIMITIVE TYPE

var a = 2 ;

var b = a ;

console.log ( b ); 

// OUTPUT  – 2 

In the PRIMITIVE TYPE the VALUE is COPIED 

If a is an OBJECT

var a = {} ;

var b = a ;

console.log ( b ); 

// OUTPUT  

 bSTORES the ADDRESS of a and POINTS to a

MUTATE 

  • TO CHANGE SOMETHING . 
  • IMMUTABLE means it CANT be CHANGED.

BY VALUE ( PRIMITIVE )

var a =3 ;

var b ;

b = a;

a = 2 ;

console.log( a ) ; 

console.log(b );

// OUTPUT

2

 

BY REFERENCE ( All OBJECTS INCLUDING FUNCTIONS can be PASSED by REFERENCE ) 

var c = { greeting : ” hi ” };

var d;

d = c;

c.greeting = “hello” ; // MUTATE 

console.log ( c.greeting);

console.log ( d.greeting);

// By REFERENCE even as PARARMETER

function changeGreeting(obj) {

obj.greeting = ” Hola” ; // MUTATE 

changeGreeting(d);

console.log(c.greeting);

console.log(d.greeting);

// OUTPUT 

Hello

Hello

Hola

Hola 

EQUAL OPERATOR SETUPS NEW MEMORY SPACE ie NEW ADDRESS

c = { greeting : ” Howdy ” };

console.log ( c.greeting );

console.log (d.greeting ) 

// OUTPUT 

Hello

Hello

Hola

Hola 

Howdy 

Hola

OBJECTS , FUNCTIONS and ‘this’  

  • INSIDE an OBJECT When the VALUE is PRIMITIVE its a PROPERTY and WHEN the VALUE is a FUNCTION its a METHOD.
  • ‘this is a GLOBAL VARIABLE and points to the GLOBAL WINDOW OBJECT  
  • So whenever you will CREATE a FUNCTION STATEMENT or a FUNCTION EXPRESSION , ‘ this ‘ variable will always POINT to the GLOBAL OBJECT ( WINDOW )

var c = {

name : ” The C OBJECT ” ,

log : function () {

console.log(this);

  }  // method

c.log() ; // INVOKING the log METHOD inside c OBJECT

INSIDE an OBJECT you can USE this VARIABLE to MUTATE its PROPERTIES

var c = {

name : ” The C OBJECT ” ,

log : function () {

 this.name = ” UPDATED C OBJECT ” ;

console.log(this);

  }  // method

c.log() ; // INVOKING the log METHOD inside c OBJECT

  • So when ‘this’ variable is USED inside an OBJECT it REFERS to that SAME OBJECT
  • Whereas when a FUNCTION is CREATED inside a METHOD and ‘this’ VARIABLE is USED in that FUNCTION ..
  • Then ‘this’ VARIABLE again POINTS back to the GLOBAL WINDOW OBJECT and OTHER PROPERTIES of the GLOBAL OBJECT,

SO IF YOU WANT TO USE THIS INSIDE A FUNCTION which is DECLARED inside a METHOD of an OBJECT the BELOW IS its SOLUTIONS

var c = { 

                 name  : ” The C OBJECT ” ,

                 log : function () {

                var self = this ;

                self.name = ‘ UPDATED C OBJECT ‘ ;

                console.log ( self ) ;

                var setname = function ( new name) {

                                                    self.name = newname;

                                         }  // FUNCTION INSIDE a  METHOD

               setname ( ” UPDATED AGAIN C OBJECT ” );

               console.log( self );

         }

};

Leave your comment

Please enter your name.
Please enter comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.