4

哪些语言提供对象字面量的使用?(或者你可以用什么语言轻松地模仿它们?)你能举一个代码例子吗?

从明显的 javascript 片段开始:

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

2 回答 2

4

签出 C# 匿名类型

var Customer = new
{
    Company = "AgileApps",
    Website = "http://www.agileapps.co.uk",
    Name = "Big Al",
    Entered = DateTime.Now
};
于 2010-12-31T11:41:49.603 回答
2

如果你用“术语”替换对象,那么 Prolog 会自然而然地做到这一点(事实上,没有其他方法可以构造一个对象)。这是一个具有二叉树的示例:

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

Lisp 和 Scheme 在这方面也有一些非常先进的特性,特别是引用和半引用:

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

几乎所有函数式编程语言都以某种形式复制了这一点。

于 2010-12-31T11:45:58.223 回答