Search code examples
programming-languagesobject-literal

What languages provide the use of object literals?


What languages provide the use of object literals? (Or in what languages could you easily emulate them?) Can you give a code example?

Starting with the obvious javascript snippet:

var someObj = {
    someProperty: 123,
    someFunction: function() {
        alert('hello!');
    }
};

Solution

  • If you replace object by "term," then Prolog does this naturally (in fact, there's no other way to construct an object). Here's an example featuring binary trees:

    % find a node in List with a nil left child and call its rightmost grandchild X
    member(node(nil,node(_,X)), List).
    

    Lisp and Scheme also have some pretty advances features in this area, particularly quoting and semiquoting:

    ;; construct right-leaning binary tree with x as the rightmost grandchild
    `(nil . (nil . ,x))
    

    Practically all functional programming languages have copied this in some form.