JavaScript Functions

“The best thing about JavaScript is the implementation of function.” -Duglous crockford
In JavaScript we don’t have method, class, constructor, modules. JavaScript have only Functions.
Functions in JavaScript are objects.
Function vs Method vs Subroutine: A method is on an object and a function is independent of an object. A subroutine does not return a value and a subroutine can change the arguments values.
# Function name is optional in JavaScript, function name is used for recursive call and debugging stack trace.
# Function objects are First Call means that they can be pass as parameter, can be returned, assigned to a variable and can be stored in an object or in an array. So we can use function like number.
# Function is an object inherited from Function.prototype and so have methods.
# Function expression and Function statement/declaration. Both looks identical as both have function keyword.
// Function Expression assigned to local variable ‘foo’
    var foo = function () { }
// Function Declaration, given the name ‘bar’
   function bar() { }
# Function is a dynamic thing so when a function statement finished it always return something either result of expression or undefined.
# In addition to the variable and parameter every function receive two pseudo parameters arguments and this.
Arguments is an array with length property and if you change in arguments the parameters also got changed.
This pseudo parameter hold the reference of object of invocation. This is one of the big code reuse idea in JavaScript where “A single inherited function can work on many objects and the inherited function know on which object it is working is by the this binding.
A Function can be called by 4 ways:
Function form
Method form
Constructor form
Apply form

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s