Function : Object
Functions are reusable pieces of code that can be executed with
different parameters.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3
----
Function([param1 : String, [param2 : String, [...]]], body : String) : Function
Same as %%#new_Function_String_String_dotdotdot_String|**new Function(param1, param2, ..., body)**%%.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.1.1
----
new Function([param1 : String, [param2 : String, [...]]], body : String) : Function
Creates a new **Function** that has the supplied parameter names and body.
If any parameter name contains a **','**, it will be split on the **','** and
each component will be added as a parameter. Unless the **body** needs to
be modified at run time, **Function**s are typically created with the
**function** keyword.
// The following result in equivalent behavior, but the first
// should be used unless the body changes at run time.
var x = function(x, y) { return x + y; };
var y = Function('x', 'y','return x + y;');
var z = Function('x, y', 'return x + y;');
var w = new Function('x', 'y', 'return x + y;');
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.2.1
----
prototype.apply([thisArg : Object, [parameters : Array]]) : Object
Call **this** with the **this** value inside the function bound to **thisArg**
and the parameters to the function from **parameters**. Returns the result
of the function call.
var whatsThis = function() { console.log(this); }
whatsThis.apply('hello');
// Call a function that takes a variable number of args
var numbers = [3, 20, 1, 45];
console.log(Math.max.apply(undefined, numbers));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.3
----
prototype.call([thisArg : Object, [param1 : Object, [param2 : Object, [...]]]]) : Object
Call **this** with the **this** value inside the function bound to **thisArg**
and parameters **(param1, param2, ...)**. Returns the result of the function call.
var whatsThis = function() { console.log(this); }
whatsThis.call('hello');
// Call a function that takes a variable number of args
console.log(Math.max.call(undefined, 3, 20, 1, 45));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.4
----
prototype.bind(thisArg : Object, [param1 : Object, [param2 : Object, [...]]]) : Function
Returns a new function that, when called, will have **this** equal to **thisArg**,
the first parameter equal to **param1**, the second parameter equal to **param2**,
etc.
var Button = function(content) {
this.content = content;
};
Button.prototype.click = function() {
console.log(this.content + ' clicked');
}
var myButton = new Button('OK');
myButton.click();
var looseClick = myButton.click;
looseClick();
var boundClick = myButton.click.bind(myButton);
boundClick();
// Example showing binding some parameters
var sum = function(a, b) {
return a + b;
};
var add5 = sum.bind(null, 5);
console.log(add5(10));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.5
----
instance.length : Number
The number of parameters specified when the function was defined.
var add = function(x, y) { return x + y; };
console.log(add.length);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.1
----
instance.prototype : Object
The prototype for objects created by calling this function
with the **new** keyword.
var Button = function(content) {
this.content = content;
};
Button.prototype.click = function() {
console.log(this.content + ' clicked');
}
var myButton = new Button('OK');
myButton.click();
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.2