blob: cab6e6d597187339693c408699fbeb6c9aa135c5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
---
title: Components.classes
slug: Components.classes
tags:
- 'XPCOM:Language Bindings'
- XPConnect
translation_of: Mozilla/Tech/XPCOM/Language_Bindings/Components.classes
---
<p> </p>
<p><code>Components.classes</code> 是一个只读的对象,其属性值是由 <a href="/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIClassInfo/contractID" title="en/ContractID">ContractID</a> 索引的。</p>
<h2 id="Introduction" name="Introduction">简介</h2>
<p><code>Components.classes</code> 是一个只读的对象,它的属性实现了 <code><a href="/zh-CN/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIJSCID" title="">nsIJSCID</a></code> 接口。每个对象都表示了 <a href="/en-US/docs/Mozilla/Tech/XPCOM" title="en/XPCOM">XPCOM</a> components 的一个 class, 而且这个 class 能够被重构或作为一个 XPCOM service 来访问。</p>
<p>该对象的属性是由 component class 的 ContractID 或 human-readable 可读的名称进行索引的。</p>
<p>All of the properties and methods of the <code><a href="/zh-CN/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIJSCID" title="">nsIJSCID</a></code> 以及其父接口 <code><a href="/zh-CN/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIJSID" title="">nsIJSID</a></code> 的所有属性和方法对于使用该对象的对象都是有效的。</p>
<p><strong>注意</strong> <code>Components.classes</code> 反映了这些组件 classes 以及能够被预先安装并且由 component manager 使用 ContractIDs 预先注册上。如果你想要使用仅仅将 <a href="/en/CID" title="en/CID">CID</a> 注册的 class, 请使用 <code><a href="/en/Components.classesByID" title="en/Components.classesByID">Components.classesByID</a></code> 替换<code>Components.classes</code> 来进行获取。</p>
<p>Note also that it is possible that a given add-on component with a given ContractID will be present on one machine but not have been installed on another machine. If the given element in the <code>Components.classes</code> object is not registered on the machine then trying to access it will generate a JavaScript warning in strict mode and the value returned will be the JavaScript value <code><a href="/en/JavaScript/Reference/Global_Objects/undefined" title="en/JavaScript/Reference/Global_Objects/undefined">undefined</a></code>. You should use the <code><a href="/en/JavaScript/Reference/Operators/Special/in" title="en/JavaScript/Reference/Operators/Special/in">in</a></code> operator to test for the element before trying to access it:</p>
<pre class="eval">if (!("@some/bogus/class;1" in Components.classes))
// do something...
</pre>
<p>The properties of the <code>Components.classes</code> object can be enumerated using a <code><a href="/en/JavaScript/Reference/Statements/for...in" title="en/JavaScript/Reference/Statements/for...in">for...in</a></code> loop.</p>
<h2 id="Usage" name="Usage">用法</h2>
<p>为了能够检索到给定 ContractID 的对象,你可以按照下面的方式来查询<code>Components.classes</code> array :</p>
<pre class="eval">var clazz0 = Components.classes["@mozilla.org/messenger;1"];
</pre>
<p><code>clazz0</code> 是针对 ContractID <code>@mozilla.org/messenger;1</code> 的 class 对象, 它通常不是由自己来使用的,但是该对象的 <code>createInstance</code> 和 <code>getService</code> 方法可用来创建 component 的一个新的实例或访问一个单例的实例,如果 contact ID 代表一种服务的话。</p>
<p>一个 <strong>新的 XPCOM component 实例</strong> 可以从通过返回的 class 对象来创建:</p>
<pre class="eval">var obj = Components.classes["@mozilla.org/supports-array;1"]
.createInstance(Components.interfaces.nsISupportsArray);
</pre>
<p>上面也可以简写成</p>
<pre class="eval">var obj = Components.classes["@mozilla.org/supports-array;1"]
.createInstance();
obj.QueryInterface(Components.interfaces.nsISupportsArray);
</pre>
<p>如果你不向 <code>createInstance() </code>提供一个特定的接口, 它就会返回一个 针对Component 的 <a href="/en/XPConnect" title="en/XPConnect">XPConnect</a> wrapper, 它会仅暴露 <code><a href="/zh-CN/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsISupports" title="">nsISupports</a></code> interface 的方法 ( 在某些特定场合下,special <a href="/en/wrappedJSObject" title="en/wrappedJSObject">wrappedJSObject</a> property 也可以被使用).</p>
<p><strong>To access a <em>service</em> </strong> (a <a href="https://zh.wikipedia.org/wiki/Singleton_pattern" title="singleton">singleton</a> component, 要使用单例模式访问组件,就应该使用 <code>getService</code> 来替代 <code>createInstance</code>:</p>
<pre class="eval"> var os = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
</pre>
<p>The first time anyone accesses a service, the corresponding component is created under the hood.</p>
<h3 id="Shortcuts" name="Shortcuts">简化写法</h3>
<p>实际操作时,一般会将 <code>Components.classes</code> 和 <code>Components.interfaces</code> 的引用存储成常量的形式:</p>
<pre class="eval">const Cc = Components.classes, Ci = Components.interfaces;
var os = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
</pre>
<p>A less known trick, useful when creating multiple instances of the same component, is to use the <code><a href="/en/JavaScript/Reference/Operators/Special/new" title="en/JavaScript/Reference/Operators/Special/new">new</a></code> operator on the class object:</p>
<pre class="eval">var clazz = Components.classes["@mozilla.org/supports-array;1"];
var inst = new clazz(Components.interfaces.nsISupportsArray);
</pre>
<p>This implicitly calls the <code>createInstance()</code> method for you. You still have to provide the interface name each time you create an instance, which is not necessary when using <code><a href="/en/Components.Constructor" title="en/Components.Constructor">Components.Constructor</a></code>.</p>
<h2 id="Determining_if_a_component_has_to_be_instantiated_or_used_as_a_service" name="Determining_if_a_component_has_to_be_instantiated_or_used_as_a_service">Determining if a component has to be instantiated or used as a service</h2>
<p>In the general case it is not possible to programmatically determine if a given component has to be instantiated or used as a service.</p>
<p>Often, this is stated in the documentation of the component you want to use. If this is not the case, you might want to try and find example usages of that component within <a class="external" href="http://mxr.mozilla.org/mozilla/search">MXR</a>.</p>
<p></p>
|