The following code-snippet is a test to see what happens when a function and a variable share the same name in the same scope. In Chrome it appears the variable definition has precedence in reference.
- Can the named function be executed, or is it completely obscured by the variable declaration?
- Is it the standard behavior in Javascript that variables take precedence over functions with the same name?
Sorry for the two part question, but it seemed wasteful to ask two separate questions.
Code:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
var overlapping = function() { return 'this is a var holding an anonymous function' };
function overlapping()
{
return 'this is a function definition';
}
output( overlapping, 'overlapping' );
output( overlapping(), 'overlapping()' );
function output( expression, description )
{
document.writeln( '<li>' + ( description ? ('<i>' + description + '</i>: ') : '' ) + expression + '</li>' );
}
</script>
</body>
</html>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…