views:

238

answers:

5

I've been doing a project at work recently focused on an almost entirely client-driven web site. Obviously Javascript is used heavily, and I've been using jQuery on top of it which has turned out to be an absolute pleasure to work with.

One of the things that has surprised me in this is how much I like the JSON object syntax and it's use within javascript (highlighted by jQuery, which uses it everywhere). for those that aren't familiar with it, consider this brief example:

function add(params) {
    params.result(params.a, params.b);
}

add({
    a: 1,
    b: 2,
    result: function(value) {
        alert(value);
    }
});

Of course, this example is extremely contrived but it illustrates the basic usage. The JSON describes an object on the fly, passed in this case as a parameter to the function, and even defines a function within it for use as a callback. I personally find this methodology very easy to use, understand, and produce APIs with (though I know there are those that would disagree with me.)

So my question is, is this type of syntax unique to javascript? I know that many languages have JSON parsers (and have used several) but they don't allow for this sort of inlined declaration. And granted, much of what you can do with this syntax can be duplicated via named parameters in various languages and lambda expressions or function pointers (Python jumps to mind), but I still don't find that quite as elegant.

Just curious, thanks for any replies!

A: 

C# 3.0 has something similar in that you can both instantiate an object using a default constructor and assign properties in the same statement. It's really just a bit of syntactic sugar, but it really make it much easier than declaring multiple constructors just to handle various combinations of parameter settings. It looks like:

 var myObject = new MyClass { Property1 = "value", NumericProperty = 1 };

There are also anonymous types in C# 3.0 which have a similar syntax; these are frequently used with LINQ

 var query = dataContext.Entity
                        .Where( e => e.Kind = 1 )
                        .Select( e => new { e.Name, e.Kind } );

This results in an IEnumerable of a new anonymous type with properties "Name" and "Kind" which, in turn, have the same types as the corresponding entity properties.

tvanfosson
A: 

Rubists would do:

def add(params) {
    params.result(params.a, params.b)
}

add({:a=> 1, :b=> 2}) do |value|
    alert(value)
end

The passing of an optional block is kind of a weird idiom in ruby. Or to be more true to your example.

def add(params) {
    params.result(params.a, params.b)
}

add({
    :a=> 1, 
    :b=> 2,
    :result=> proc do |value|
        alert(value)
    end
})

But the concept of the inline function originates from lambda calculus and LISP.

toby
+2  A: 

The canonical example is Lisp: in Lisp, the language isn't even defined in terms of text, it is defined in terms of data structures. There really is not difference between code and data: code is just a list whose first element is interpreted as an operation and the rest as operands.

Writing a program is just writing lists.

Jörg W Mittag
+1  A: 

Lua uses "tables" which are pretty much identical to javascript objects.

Luca Matteis
+1  A: 

Syntax of JSON is very similar to Perl definition of complex structures. And your example would be:

#!/usr/bin/perl

use strict;
use warnings;
use signatures;
sub add($hash) {
    $hash->{result}($hash->{a}, $hash->{b});
}

add({
    a=> 1,
    b=> 2,
    result=> sub ($val) {
        print "$val\n";
    }
});
Alexandr Ciornii