Function object that is stored in the property named prototype is a constructor. <!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>Javascript Function-object constructor</title>
<script type="text/javascript">
function main() {
var f = new Function("a", "b", "c", "return (a+b) * c;");
document.getElementById('out').innerHTML = f(2, 4, 7);
}
</script>
</head>
<body onload='main();'>
<div id='out'>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type='text/javascript' src='matrix.js'></script>
<script type='text/javascript'>
var out;
function print_out(text) {
out.innerHTML += '<br>' + text;
}
function init() {
out = document.getElementById('out');
var f = function(a, b, c) {alert(a + b + c)};
print_out('typeof(f): ' + typeof(f));
print_out('f: ' + "<code>" + f + "</code>");
print_out('typeof(f.prototype): ' + typeof(f.prototype));
print_out('<b>Properties of a function:</b>');
var f_props = Object.getOwnPropertyNames(f);
for (var elem in f_props) {
print_out('> ' + f_props[elem] + ": " + f[f_props[elem]]);
}
print_out('the end');
}
</script>
</head>
<body onLoad='init()'>
<div id='out'>
<b>Out</b>:
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>Closures</title>
</head>
<script type="text/javascript">
function create_function (a) {
return function() {
return a;
}
}
function create_functions() {
var f = {};
var x;
for (var i=0; i<10; i++) {
f['func_' + i] = function() {
return i;
}
x = i;
f['FUNC_' + i] = function() {
return x;
}
f['cnuf_' + i] = create_function(i);
}
return f;
}
function show(text) {
document.getElementById('out').innerHTML += text + "<br>";
}
function main() {
funcs = create_functions();
show( funcs.func_1() ); // 10
show( funcs.func_5() ); // 10
show( funcs.FUNC_1() ); // 9
show( funcs.FUNC_5() ); // 9
show( funcs.cnuf_1() ); // 1
show( funcs.cnuf_5() ); // 5
}
</script>
<body onload='main();'>
<div id='out'>
</div>
</body>
</html>