Wednesday, April 25, 2012

In Javascript, function can be assigned to variable


Example to to assign a Javascript function to a variable like a object.



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mobile-Web-App: Javascript Exercise - function</title>
<script>

var func = function(i){
 return i*2;
}

function test(){

info = "<p> typeof(func): " + typeof(func) + "</p>"
 + "<p> typeof(func(5)): " + typeof(func(5)) + "</p>"
 + "<p> func(5): " + func(5) + "</p>";

document.getElementById("prompt").innerHTML=info;
}

</script>
</head>
<body onload="test();">
<h1>Mobile-Web-App: Javascript Exercise - function</h1>
<div id="prompt"></div>
</body>
</html>


No comments:

Post a Comment