Monday, January 21, 2008

Javascript function and anonymous function differrence 1

Javascript函数存在于定义它们的整个作用域(包括出现在该函数语句前面的语句)内。与之相反, Javascript匿名函数只是为后续的语句定义的。如下example:


<html>
<head>
<title> test stateFunction and expressionFunction difference</title>
</head>
<body>
<div id="d1">d1</div>
<div id="d2">d2</div>
doc_write is not a function<br />
[Break on this error] doc_write(document.getElementById('d1').innerHTML);
<script type="text/javascript" charset="utf-8">
doc(document.getElementById('d1').innerHTML);
doc_write(document.getElementById('d1').innerHTML);

function doc(argument) {
document.write("<p>\n");
document.write(argument);
document.write("</p>\n");
}

var doc_write = function(args) {
document.write("<p>\n");
document.write(args);
document.write("</p>\n");
};
</script>
</body>
</html>

No comments :