When I wrote Java, Groovy and (J)Ruby, Owen Densmore and Lawrence Oluyede suggested that I should include Jython as well. Here it is.... JavaScript and Jython for Java programmers. Note that while I am reasonably comfortable with Java and JavaScript, I don't know much about Jython. If you find anything wrong here, please let me know. I used Jython version 2.1. For JavaScript, I used Rhino 1.6R2 that is co-bundled with JDK 6. The jsr-223 script engine for Jython and many other languages are avaiable at http://scripting.dev.java.net
| Feature | Java | JavaScript | Jython |
|---|---|---|---|
| Type System | Static with few dynamic checks inserted as needed. | Dynamic | Dynamic | Comments |
|
|
No multiple line comments. But, Owen Densmore notes that multiline
string literals may be used instead.
|
| Control Statements |
* break can be used in
loops to break out of loop. * continue statement skips the current iteration of a for, while, or do-while loop and valuates the boolean expression that controls the loop. * Labeled break and continue |
* null is treated as false. * undefined is treated as false. * Unlike Java, switch statement works for arbitrary expressions (not just for integral expressions and enums as in Java). * break can be used in loops to break out of loop. * continue statement skips the current iteration of a for, while, or do-while loop and valuates the boolean expression that controls the loop. * Labeled break and continue as in Java are supported. |
* for statement iterates over the items of any
sequence (a list or a string)* Note that statement grouping is done by indentation. No explicit "{" "}" around group of statements. * The break statement, like in Java, breaks out of the smallest enclosing for or while loop. * The continue statement, like in Java, continues with the next iteration. * do..while is not supported. * Loop statements may have an else clause which is executed when the loop terminates normally (i.e., not when "break")
|
| String Literals | "hello world" |
* Need not escape " within single-quoted string* Need not escape ' within double-quoted string |
* Need not escape " within single-quoted string* Need not escape ' within double-quoted string * Thanks to Owen Densmore for the info. about multi-line string literals |
| Defining functions | Not supported. Everything has to be inside a class. Use static methods instead. |
* functions may be unnamed (anonymous functions)* functions are first-class values - can be passed, assigned and returned as values. * functions may be nested inside other functions * nested function can refer to local variables of the enclosing functions
|
* functions are first-class values - can be passed,
assigned and returned as values.* functions may be nested inside other functions * nested function can refer to local variables of the enclosing functions [closures] * functions may be unnamed (anonymous functions called "lambdas") * lambdas are restricted -- the body of lambdas can only be expressions. lambda definition does not include a "return" statement -- it always contains an expression which is returned
Without the "from __future__ import nested_scopes",
nested scopes don't work as "expected" normally.
This behaviour depends on Python version - refer to
What's New in Python 2.1 by A.M. Kuchling and
Back to the __future__
|
| Class Declaration |
|
JavaScript supports prototype-based object orientation. No direct support for classes. JavaScript supports first -class functions. Any function can serve as object constructor. An object is an associative array - with keys being either string or integer. The values could be strings, numbers, object or even function (which then becomes a "method" of the object).
There is
JavaScript framework called "Prototype" that supports class
based object orientation with JavaScript.
|
Classes can have executable statements (like print call in
above example).
|
| Instance Variables |
|
No need to declare instance variables. No special
syntax for instance variable names. Just introduce
instance variable by
Typically, constructor functions initialize instance
variables. All fields are "public". If you want private instance
variables and methods, you may want to
read Private Members in JavaScript
|
No need to declare instance variables. No special
syntax for instance variable names.
|
| Static Variables |
|
There are no classes in JavaScript. But, every object
has a prototype -- identified by a special property (or field)
called __proto__. The prototype
property is set when the object is created and this property
can be changed later if needed. The "static" fields can be
stored in the prototype object.
You may want to read Private Static Members in Javascript
as well.
|
Class variables are declared
within the "class".
Note: Unlike Java, all variables declared inside class
declaration are class variables. Instance variables are
initialized only within the instance methods.
|
| Global Variables | Not supported. Use public static final variables in a class or use enum as appropriate. | Top-level variables are supported. No special naming enforced. No need to declare and can be "defined" by initialization. Actually, the "globals" are contained in "Global" object - which contains all "global" definitions - variables and (global) functions. "this" in glabal scope refers to Global object. | Top-level variables are supported. No special syntax for global variable names. |
| Method Definition |
|
To define an instance method:
add a function valued property
to prototype [of the constructor
function]
|
Note: Unlike Java and JavaScript, you have
to specify special "self" parameter explicitly.
|
| Static Method Definition |
|
There are no classes in JavaScript. But, every object has a prototype -- identified by a special property (or field) called __proto__. You may want to read Private Static Members in Javascript for variety of way to code static members. | You may want to read The Static Method Thing |
| Returning from a method |
|
|
|
| Object Creation |
|
|
Use class name like as a function name.
|
| Method Call |
|
|
|
| Referring to the current object | this | this | self |
| Object Initialization |
|
Objects are created by constructor functions!
|
|
| Extending another class |
|
Every JavaScript has a prototype. This can be accessed via __proto__ field. When a property is not found in an object, it's prototype is searched. If not, prototype of the prototype is searched and so on - till prototype chain ends with an object with null __proto__. You can have Classical Inheritance in JavaScript or use JavaScript "Prototype" framework. |
* Jython supports multiple inheritance. User specifies
zero or more superclasses within the parans just after
the class name.
|
| Null | null | null | None |
| Arrays |
|
|
|
| Array Literals |
|
|
|
| Array length | arr.length |
|
len(arr) |
| Hash Literals | Not supported |
Use JavaScript Object Literals. Objects
are associative arrays.
|
|
| varargs methods |
Use JDK 1.5+. The "..." after the last formal
argument means this is a vararg method.
|
JavaScript functions can accept any number of
arguments. "arguments" object is filled with all
arguments.
|
The "excess" args are packed as tuple and passed.
|
| Keyword arguments (a.k.a Named Parameters | Not supported |
Not supported. You can accept Object but pass Object
literals from caller.
|
Functions can be called with keyword arguments of
the form "keyword = value"
|
| Operator Overloading | Not supported - except for built-in String + operator | Not supported - except for built-in String + operator |
Specially named operator methods are
defined by class author.
|
| Referring to super class method | super.foo(); | JavaScript supports prototypes for objects. It is possible to get any method anywhere in the prototype chain (for example: obj.__proto__.__proto__.func where "func" is a function valued property defined in the prototype of prototype of obj) and then explicitly apply that on "this" object. |
|
| Exception Handling |
|
Exception handling is similar as Java - except that
anything (including string, number) can be thrown.
Also, catch clause cannot specify "exception type".
But as lopex comments, there may be
multiple catch clauses (which seems to be an extension of the ECMAScript-262 standard).
Thanks to lopex for notifying the multiple catch clauses.
|
Thanks to Lawrence Oluyede for clarifying the "else:"
usage. So "else:" clause code is executed only
if exception was not thrown in try block. There is also
finally: clause -- similar
to finally in Java. See Lawrence Oluyede's comment in
this entry for CPython/Jython version specific behaviour.
Also, you may want to refer to Errors and Exceptions section of the Python tutorial as well.
|
| Modules, Namespaces |
There may be
future
improvements in this area.
|
No explicit support for modules/packages. Use objects for everything. For example, you may use this idea. Or similar to the package system of Dojo. Similarly, Phobos webapp framework has it's own namespace support for JavaScript. |
A Python module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module's name is available as the value of the global variable __name__.
Refer Modules section
of Python tutorial.
|
Posted by stotti.blog() on September 26, 2006 at 05:56 PM IST #
Posted by lopex on September 27, 2006 at 03:34 AM IST #
So adding
"""
This is a multi-
line comment
"""
to a jython program should work just fine.
.. And thanks for adding the new entry for Jython!
Owen
Posted by Owen Densmore on September 27, 2006 at 08:34 AM IST #
Posted by Lawrence Oluyede on September 27, 2006 at 02:37 PM IST #
Posted by Mike Norman on September 27, 2006 at 06:43 PM IST #
Posted by A. Sundararajan on September 27, 2006 at 06:50 PM IST #