The Function Type
Functions are objects. Each function is an instance of the Function type. function type has properties and methods. function names are pointers to function objects.
Functions are typically defined using function-declaration syntax.
function sum (num1, num2) {
return num1 + num2;
}
We can rewrite the code above as:
var sum = function(num1, num2){
return num1 + num2;
};
The following code uses the Function constructor:
var sum = new Function("num1", "num2", "return num1 + num2");
Multiple names for a single function:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var addSomeNumber = function (num){
return num + 100;
};
addSomeNumber = function (num) {
return num + 200;
};
var result = addSomeNumber(100); //300
document.writeln(result);
</script>
</head>
<body>
</body>
</html>
From the code you can see that the last function wins.
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Function:
- The Function Type
- Function Declarations versus Function Expressions
- Functions as Values
- Returning a function from a function
- Function arguments
- this for function context
- Function caller
- Function length property
- Function apply()
- Function.call() method
- Function's bind() method
- Function toLocaleString() and toString()