Function
JavaScript functions are objects. They can be defined using the
Function
constructor, like so:
var sum = new Function('a', 'b', 'return a + b;');
This is a (generally not recommended) alternative to the function literal (also known as function expression):
var sum = function (a, b) { return a + b; };
Or, the more common function definition:
function sum(a, b) { return a + b; }
The Function.prototype members
Property/Method |
Description |
---|---|
|
Allows you to call another function while overwriting the other function's this value. The first parameter that function whatIsIt(){ return this.toString(); } > var myObj = {}; > whatIsIt.apply(myObj); "[object Object]" > whatIsIt.apply(window); "[object Window]" |
|
The same as |
|
The number of parameters the function expects. > parseInt.length;
2
If you forget the difference between call() and apply(): > Function.prototype.call.length; 1 > Function.prototype.apply.length; 2 The call() property's length is 1 because all arguments except the first one are optional. |
ECMAScript 5 additions to a function
Property/method |
Description |
---|---|
|
When you want to call a function that uses this internally and you want to define what this is. The methods > whatIsIt.apply(window);
"[object Window]"
|