X

Sundararajan's Weblog

  • Java
    September 4, 2006

Why not JavaScript?

Guest Author

I just came across this post: Java isn't just Python without the cool language features - Java platform as such does not miss cool scripting features. While the examples suggested are Jython Groovy and JRuby, I'd like to point out JavaScript can be added to the list as well - in particular because JDK 6 includes Rhino based JavaScript engine and JavaScript does support these:


  1. First-class functions - yes
    • Functions can be passed, returned and stored as values
    • Function literals (anonymous functions) supported
    • Can create Function values from code strings (Function.prototype)
  2. Default parameters - Yes (indirectly)

    JavaScript function arity is not fixed. A function can always get all arguments by arguments object. Also, function can check for undefined values.


    function mul(x, y) {
    // if 'y' is passed assume default value of '3'
    if (y == undefined) y = 3;
    return x \* y;
    }

  3. Tuples - Yes, JavaScript arrays are dynamically sized. length property can be assigned. Java arrays can also be accessed with the same syntax. JavaScript arrays and objects are treated uniformly. Object fields and methods can be accessed using array index operator with string keys.
  4. Closure support - yes

    Nested functions are supported -- "inner" function can be returned and stored. Inner functions have the "outer" function's local scope as "parent" scope and therefore can access locals of the outer function. If you are looking for Smalltalk-like block closures with special "return" instruction to return from enclosing function, then you have to simulate that by exceptions.

  5. Continuations - although not yet part of JavaScript standard Rhino implementation supports continuations
  6. Meta-Programming/reflection - yes
    • Rhino supports assignable "protos" (__proto__ property)
    • Walking all "fields"/"methods" of an object by "for..in" loop
    • "eval" code in a string
    • Changing parent scope by assignable __parent__ property

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.