[<<][webdev][>>][..]
Thu Mar 18 15:33:58 CET 2010

Rhino (just JavaScript)

java -classpath ~/sw/bin/js.jar  org.mozilla.javascript.tools.shell.Main

// lambda: abstraction and application
js> (function(a){return 1+a;})(1)
2

// objects
js> function Test() { this.foo = 123; }
js> new Test()
[object Object]
js> (new Test()).constructor
js> (new Test()).bar
js> (new Test()).foo
123
js> (new Test()).bar == false
false
js> (new Test()).bar == undefined
true

// It seems that capitalized constructors is just a convention.
js> function foo() { this.bar = 123; }
js> new foo()
[object Object]

// The global object (namespace).
js> this
[object global]
js> (function() {return this;})()
[object global]

// Each object is a separate namespace.
js> function foo() { this.bar = 123; this.foo = function() {return this;}}
js> (new foo()).foo()
[object Object]

// Prototypes: add a .prototype object in the constructor
js> foo.prototype
[object Object]
js> foo.prototype.abc = 1
1
js> (new foo()).abc
1

// Reflection: list properties.  Iteration over an object (associative
// array) returns a collection of strings representing slot names.

js> for (att in new foo()) { print (att); }
bar
foo
abc

// I.e. cfr arrays:
js> for (att in ["1","2","3"]) { print (att); }
0
1
2

// Data definition: JSON
js> var a = {"foo" : 123, "bar" : 345}

// Object (associative array) indexing
js> for (it in a) { print(it,a[it]); }
foo 123
bar 345

// Quotes are optional
js> var a = {foo : 123, bar : 345}


// Applying functions (methods) to objects.  Can use call or apply.

js> obj = new Object()
[object Object]
js> obj.x = 123
123
js> send = function(obj,msg) { return msg.call(obj); }

function (obj, msg) {
    return msg.call(obj);
}

js> send(obj, function() {return this.x;})
123
js> send2 = function(obj,msg) { return msg.apply(obj,[]); }

function (obj, msg) {
    return msg.apply(obj, []);
}

js> send2(obj, function() {return this.x;})
123


[Reply][About]
[<<][webdev][>>][..]