Can create a new Function object dynamically, but suffers from security & some performance issues to 'eval'. Creates functions that execute in global scope only. Usage: new Function('arg1','arg2'..., '// function body code')
Normal declaration / expression: function name(p1, p2) { // statements} // name can be omitted in expression
Generator/generator expression: function* name(p1, p2) {// statements }
Arrow expression: (p1, p2) => {//statements} // note array function has no own 'this', 'arguments' and 'super', suitable for pure function and not suitable for methods and cannot be used for constructors
Constructors: new Function(..) see above, new GeneratorFunction(arg1, arg2, ... argN, functionBody)
Normal ones: p1, p2, .... pN - missing parameters are passed 'undefined'
With default: (p1, p2='default',...) if received 'undefined' will use default to replace
Destruct syntax: ( { para1, para2})
Rest parameters: function sum(...theArgs) { // theArgs is passed as an array}
arguments: array-like object containing arguments
arguments.length:
X arguments.callee: deprecated, the current executing function
X arguments.caller: deprecated
this:
A method itself is just a function not bound (no "this").
When called with an object in syntax "someObject.someMethod", "this" is bound - to "someObject"
Therefore, (IMPORTANT) a method can be passed as a callback - but when caller calls the method without binding something (caller just views it as a function), "this" will be missing! So if a callback needs to access "this", use "this.someMethod.bind(this)"
Properties attached to Function.prototype
arguments: deprecated, use 'arguments' object within function body instead
arity: deprecated
caller: not standard and not on track
length: number of expected arguments
name: function name
Function.displayName: not standard
Function.prototype.constructor: ??? just don't know what's the use of this
These are Function.prototype properties...
apply(thisObject, [arg1, arg2, arg3]) - pass in 'this' object and the arguments, and invoke the function
call(thisObject, arg1, arg2, arg3...) - different in 'apply' in accepting an argument list while 'apply' accepting a single array of arguments
bind(thisObject, arg1, arg2...) - returns a new 'bind function' that when called, call the original function with 'thisObject' as 'this' and also arg1, arg2...., and any arguments given to the 'bind function' will follow
isGenerator() - note: has not been standardized
toSource() - has not been standardized
toString() - returns a string representation of the source code
Can be exited and later re-entered, context preserved