0

为什么这个 HTML/脚本(来自“JavaScript Ninja 的秘密”)不呈现?

http://jsfiddle.net/BCL54/

<html>
    <head>
        <script>
            function outer(){

  var a = 1;

  function inner(){ /* does nothing */ }

  var b = 2;

  if (a == 1) {
    var c = 3;
  }

}

outer();

assert(true,"some descriptive text");
assert(typeof outer==='function',
      "outer() is in scope");
assert(typeof inner==='function',
      "inner() is in scope");
assert(typeof a==='number',
      "a is in scope");
assert(typeof b==='number',
      "b is in scope");
assert(typeof c==='number',
      "c is in scope");
        </script>
    </head>
    <body>        
    </body>
</html>
4

1 回答 1

3

因为您没有导入包含assert函数的 Resig 必要脚本:

<script>
function assert(pass, msg){
  var type = pass ? "PASS" : "FAIL";
  jQuery("#results").append("<li class='" + type + "'><b>" + type + "</b> " + msg + "</li>");
}
function error(msg){
  jQuery("#results").append("<li class='ERROR'><b>ERROR</b> " + msg + "</li>");
}
function log(){
  var msg = "";
  for ( var i = 0; i < arguments.length; i++ ) {
    msg += " " + arguments[i];
  }
  jQuery("#results").append("<li class='LOG'><b>LOG</b> " + msg + "</li>");
}
</script>

您可以在他的站点的源代码中找到这些功能。请注意,这些函数还要求写入 jQuery 和一些 DOM 元素。你最好适应你的页面。

除非您精通 javascript 以重写这些函数,否则您最好直接在网站上进行出色的练习。

于 2013-02-07T19:56:11.067 回答