Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
Last active August 29, 2015 14:07
Show Gist options
  • Save lizlongnc/6e7cddbac1f8f75c226b to your computer and use it in GitHub Desktop.
Save lizlongnc/6e7cddbac1f8f75c226b to your computer and use it in GitHub Desktop.
JS Functions
Where did functions come from?
subroutines
In earlier languages, subroutines were written and used in various ways.
Subroutines were called and could return something.
They were also referred to as: sub, procedure, proc, func, functions, lambda and are all the same idea.
What advantages do subroutines give us?
code reuse which was originally a memory conserving device
decomposition - take a complicated problem and break it down into subroutines
modularity
expressiveness
higher order where functions become parameters and/or return values which gives programming a lot of power
In JS adding something to a prototype is extremely efficient, because the prototypes link to it, they do not copy from it.
So you can have 1 function that is linked to thousands of objects and you only pay for the 1 function.
However, the deeper your prototype chain, the longer it will take to resolve. Prototype chains tend to be much shorter than class hierarchy found in other OOP languages.
In JS functions are dynamic unlike in some languages where they are static, so where functions are declared is important in JS.
Declare all functions before you call them. Declare all variables at the top of the function. JS provides mechanisms that allow you to ignore this advice, but they are problematic.
Every function in JS returns something whether or not a return statement is explicitly used.
return express;
or
return;
If there is no expression, then the return value is undefined.
Except for constructors, whose default return value is this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment