4

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!');
    }
};
skaffman
  • 398,947
  • 96
  • 818
  • 769
ivo
  • 4,101
  • 5
  • 33
  • 42

2 Answers2

4

Checkout C# anonymous types

var Customer = new
{
    Company = "AgileApps",
    Website = "http://www.agileapps.co.uk",
    Name = "Big Al",
    Entered = DateTime.Now
};
jenson-button-event
  • 18,101
  • 11
  • 89
  • 155
2

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836