<p>This tests verifies the scope of a function's name. | |
</p> | |
<pre id="console"></pre> | |
<script> | |
function log(s) | |
{ | |
document.getElementById("console").appendChild(document.createTextNode(s + "\n")); | |
} | |
function shouldBe(a, aDescription, b) | |
{ | |
if (a == b) { | |
log("PASS: " + aDescription + " should be " + b + " and is."); | |
return; | |
} | |
log("FAIL: " + aDescription + " should be " + b + " but instead is " + a + "."); | |
} | |
if (window.testRunner) { | |
testRunner.dumpAsText(); | |
} | |
// Function declarations do not put the function's name in scope. | |
function f() | |
{ | |
shouldBe( | |
f.name == 'f', | |
"f.name == 'f'", | |
true | |
); | |
f = 1; | |
shouldBe( | |
f == 1, | |
"f == 1", | |
true | |
); | |
}; | |
f(); | |
// Function expressions do put the function's name in scope, as a read-only property. | |
var g = function g() | |
{ | |
shouldBe( | |
g.name == 'g', | |
"g.name == 'g'", | |
true | |
); | |
g = 1; | |
shouldBe( | |
g == arguments.callee, | |
"g == arguments.callee", | |
true | |
); | |
}; | |
g(); | |
</script> |