`
jiagou
  • 浏览: 2531216 次
文章分类
社区版块
存档分类
最新评论

W3C Web IDL

 
阅读更多


Web IDL(Interface Definition Language,接口定义语言)该规范定义了一个OMG IDL 3.0的语法子集,用来规范定义的接口。Web IDL 是一个具有多种功能的IDL 变量,便于规范Web平台中的常用脚本对象的操作。其中包括了大量的扩展功能,以及捆绑了针对ECMAScript 3rd Edition 和Java的语言工具。

《Web IDL》工作草案,即过去的《DOM规范语言绑定(Language Bindings for DOM Specifications)》。该工作草案定义了一个OMG IDL 3.0版的语法子集,供那些需要定义接口的规范使用。InfoQ采访了该规范的编辑,以进一步了解该规范、以及该规范对Web开发社区的影响。

由该工作草案定义的接口定义语言(interface definition language)叫作Web IDL,它可被用来描述要在Web浏览器里实现的接口。Web IDL是一种IDL的变体,它所具有的很多特性使之能够更容易地对Web平台里的常用脚本对象的行为进行规定。为了支持过去只能以文字描述的常用功能,该IDL在很多方面得到了扩展。另外,它还为ECMAScript第3版和Java给出了精确的语言绑定。该规范文档在介绍部分解释了为何要有这样一个规范:

过去,W3C技术报告都是用对象管理组织(OMG)的接口定义语言(IDL)[OMGIDL]来描述编程语言接口的。利用该IDL,可以以独立于语言的方 式来描述这些接口。通常,这些文档还要在附录中另外给出一个语言绑定,以详细描述用IDL描述的接口如何与给定语言的构造成分相对应。

然 而,这些规范在为ECMAScript这一Web上最常用的语言给出绑定时,描述的精确度很低,因而造成互操作性问题。另外,各个规范必须描述同样的基本信 息,比如:用IDL描述的DOM接口相当于ECMAScript全局对象的属性,或者无符号长整数IDL类型映射为ECMAScript的数值类型,等 等。
Web IDL的目的,就是详细定义一种语言,以便W3C规范定义接口、并为这种接口的ECMAScript与Java绑定提供精确的一致性要求 (conformance requirements)。W3C期望该文档对已发布规范的实现者们起到指南作用,并期望以后发布的规范参照本文档,以确保合乎规范的各接口实现之间能 够互操作。“这对《HTML 5》的制定将有极大的帮助”,另外,《XMLHttpRequest Level 2》的制定也将参照Web IDL规范。


下面为 web idl 接口定义 Student对象及在js代码中作为内部对象使用的例子。

The following IDL fragment defines an interface that has custom stringification behavior that is not specified in the IDL itself.

IDL
[Constructor]
interface Student {
  attribute unsigned long id;
  attribute DOMString familyName;
  attribute DOMString givenName;

  stringifier DOMString ();
};
Thus, prose is required to explain the stringification behavior, such as the following paragraph:

Objects that implement the Student interface must stringify as follows. If the value of the familyName attribute is null, the stringification of the object is the value of the givenName attribute. Otherwise, if the value of the familyName attribute is not null, the stringification of the object is the concatenation of the value of the givenName attribute, a single space character, and the value of the familyName attribute.

An ECMAScript implementation of the IDL would behave as follows:

ECMAScript
var s = new Student();
s.id = 12345679;
s.familyName = 'Smithee';
s.givenName = 'Alan';

var greeting = 'Hi ' + s;  // Now greeting == 'Hi Alan Smithee'.






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics