Freigeben über


Pop Quiz - JavaScript for Fun

Most people could write something in JavaScript, though they barely made it correct.

I started using JavaScript while I was in school, and the project I gave myself was to implement a Scheme interpreter that runs in web browsers. As a result, I've realized the tight relationship between JavaScript and Scheme, I became a fan of JavaScript and have been using it a lot.

Before the quiz, let me tell a brief story of JavaScript.

The JavaScript language was implemented by Brendan Eich of Netscape, soon Microsoft developed a dialect called JScript and shipped with Internet Explorer in 1996. Later in the same year the standardization started, and the first version was authored by Guy Steele, who also created Scheme and authored the Java language specification. The specification was named as ECMA-262, since the specification came after JavaScript and JScript, in order to compromise among Netscape, Microsoft and Sun, a new name ECMAScript was picked.

Despite of it's Java-like syntax, JavaScript shares many concepts with functional programming language. This mixture makes JavaScript easy to use (also easy to misuse), expressive, powerful and popular.

Microsoft has a long history of providing JavaScript implementations, including JScript, JScript.NET and Chakra. While the old JScript engine is in sustaining engineering mode, JScript.NET seems to be deprecated, it's quite promising that Chakra has been powering both the latest Internet Explorer and Windows Runtime.

Now let's put the question - what would you get from the following page?


 <html>
<head>
  <title>JavaScript for Fun</title>
  <script type="text/javascript">
    var S = {};
    for (var N in (function () { return this; }).call(function (_) { return _; }()))
      S[N] = true;
    this.undefined = 1;
    undefined = 2;
    var undefined = 3;
    var A = this.undefined;
    this.B = undefined;
</script>
</head>
<body>
  <script type="text/javascript">
    var C = 'global var C';
    this.D = 'global this.D';
    function F() { };
    this.G = function () {
      return (function () { return this; }).call(function (_) { return _; }());
    };
    this.H = function () { };
    delete F;
    delete G;
    eval('var Z = "global eval var"');
    (function (global) {
      var undefined = 'defined';
      E = undefined;
      var X = 'local var X';
      this.Y = 'local this.Y';
      delete H;
      eval('I = "it just works"');
      eval('var J = "local eval var"');
      alert((function (O) {
        var R = [];
        for (var M in O)
          if (!S.hasOwnProperty(M))
            R.push(M + ': ' + String(O[M]));
        return '[' + R.sort().join(', ') + ']';
      })(global));
    })(this);
    var K = 'global var K';
</script>
</body>
</html>