« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today
XML

Blog::Navigation

GetJava Download Button
Get the Source
Personal Blog

Blog::Referers

Today's Page Hits: 932

Powered by Roller Weblogger.
« invokespecialdynamic... | Main | Bex script »
20060904 Monday September 04, 2006

Why not JavaScript?

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



( Sep 04 2006, 08:55:48 PM IST ) Permalink del.icio.us | furl | simpy | slashdot | technorati | digg

Comments:

Post a Comment:

Comments are closed for this entry.
Copyright (C) 2005, A. Sundararajan's Weblog