# Function

## Overview

`JavaScript functions` have key properties like `constructor`, which specifies the function that creates an object's prototype, and `length`, which indicates the number of expected arguments. These properties allow developers to inspect and manage function behaviors.

Methods such as `apply()`, `bind()`, `call()`, and `toString()` provide flexible ways to manipulate function contexts, invoke functions with specific arguments, and retrieve their source code.

Practical examples demonstrate how these features enhance function utility in `Servoy applications`.

Each function in javascript is actually a Function instance.

For more information see: [Function (MDN)](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function).

## Properties Summarized

| Type                                                         | Name                        | Summary                                                     |
| ------------------------------------------------------------ | --------------------------- | ----------------------------------------------------------- |
| [Function](/reference/servoycore/dev-api/js-lib/function.md) | [constructor](#constructor) | Specifies the function that creates an object's prototype.  |
| [Number](/reference/servoycore/dev-api/js-lib/number.md)     | [length](#length)           | Specifies the number of arguments expected by the function. |

## Methods Summarized

| Type                                                     | Name                    | Summary                                                                                                                                                                                                 |
| -------------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| void                                                     | [apply()](#apply)       | Applies the method of another object in the context of a different object (the calling object); arguments can be passed as an Array object.                                                             |
| void                                                     | [bind()](#bind)         | Creates a new function which, when called, itself calls this function in the context of the provided value, with a given sequence of arguments preceding any provided when the new function was called. |
| void                                                     | [call()](#call)         | Calls (executes) a method of another object in the context of a different object (the calling object); arguments can be passed as they are.                                                             |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [toString()](#tostring) | Returns a string representing the source code of the function.                                                                                                                                          |

## Properties Detailed

### constructor

Specifies the function that creates an object's prototype.

**Type**\
[Function](/reference/servoycore/dev-api/js-lib/function.md)

**Sample**

```js
function Tree(name) {
	this.name = name;
}
theTree = new Tree("Redwood");
console.log("theTree.constructor is " + theTree.constructor);
```

### length

Specifies the number of arguments expected by the function.

**Type**\
[Number](/reference/servoycore/dev-api/js-lib/number.md)

**Sample**

```js
function addNumbers(x, y){
	if (addNumbers.length == 2) {
		return (x + y);
	} else
		return 0;
}
```

## Methods Detailed

### apply()

Applies the method of another object in the context of a different object (the calling object); arguments can be passed as an Array object.

**Returns:** void

**Sample**

```js
function book(name, author) {
	this.name = name;
	this.author = author;
}

function book_with_topic(name, author, topic) {
	this.topic = topic;
	book.apply(this, arguments);
}
book_with_topic.prototype = new book();

var aBook = new book_with_topic("name","author","topic");
```

### bind()

Creates a new function which, when called, itself calls this function in the context of the provided value, with a given sequence of arguments preceding any provided when the new function was called.

**Returns:** void

**Sample**

```js
var x = 9,
	module = {
		getX: function() {
		     return this.x;
		},
		x: 81
	};
//  "module.getX()" called, "module" is "this", "module.x" is returned
module.getX(); // > 81
//  "getX()" called, "this" is global, "x" is returned
getX(); // > 9
//  store a reference with "module" bound as "this"
var boundGetX = getX.bind(module);
//  "boundGetX()" called, "module" is "this" again, "module.x" is returned
boundGetX(); // > 81
```

### call()

Calls (executes) a method of another object in the context of a different object (the calling object); arguments can be passed as they are.

**Returns:** void

**Sample**

```js
function book(name) {
	this.name = name;
}

function book_with_author(name, author) {
	this.author = author;
	book.call(this, name);
}
book_with_author.prototype = new book();

var aBook = new book_with_author("name","author");
```

### toString()

Returns a string representing the source code of the function. Overrides the Object.toString method.

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md)

**Sample**

```js
function printHello() {
	return "Hello";
}
application.output(printHello.toString());
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
