Last active
August 29, 2015 14:07
-
-
Save lizlongnc/9b44cc300ddd784a4dbd to your computer and use it in GitHub Desktop.
JS Function Invocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The () suffix operator surrounding zero or more comma separated arguments. | |
The arguments in () passed to the function will be bound to the JS function pseudo parameter - parameters. | |
If a function is called with too many arguments, the extra arguments are ignored. | |
If a function is called with too few arguments, the missing values will be undefined. | |
There is no implicit type checking on the arguments. | |
There are 4 ways to call a function: | |
Function form | |
functionObject(arguments) | |
When a function is called in the function form, this is bound to the global object. | |
That is not very useful. (Fixed in ES5/Strict) | |
An inner function does not get accss to the outer this. | |
so the following was used and may see in older code: | |
var that = this; | |
ECMAScript6 - the keyword this will be bound to undefined | |
Method form | |
thisObject.methodName(arguments) | |
thisObject["methodName"](arguments) | |
When a function is called in the method form, this is set to thisObject, the object containing the function | |
This allows methods to have a reference to the object of interest. | |
Constructor form | |
new FunctionObject(arguments) | |
Looks just like the Function form except has the new prefix. | |
When a function is called with the new operator, a new object is created and assigned to this. | |
If there is not an explicit return value, then this will be returned. | |
Used in the Pseudoclassical style. | |
Apply form | |
functionObject.apply(thisObject, [arguments]) | |
A function's apply or call method allows for calling the function, explicitly specifying thisObject. | |
It can also take an array of parameters or a sequence of paramenters. | |
Function.prototype.call = function (thisObject) { | |
return this.apply(thisObject, Array | |
.prototype.slice.apply(arguments, [1])); | |
}; | |
What this will be will depend on the invocation form. | |
Invocation form: this | |
function the global object or undefined, depending on version of JS | |
method the object | |
constructor the new object | |
apply you explicitly say what it's gonna be by using an argument | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment