aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/mozilla/javascript_tips/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/mozilla/javascript_tips/index.html')
-rw-r--r--files/zh-cn/mozilla/javascript_tips/index.html112
1 files changed, 0 insertions, 112 deletions
diff --git a/files/zh-cn/mozilla/javascript_tips/index.html b/files/zh-cn/mozilla/javascript_tips/index.html
deleted file mode 100644
index 2de76fff08..0000000000
--- a/files/zh-cn/mozilla/javascript_tips/index.html
+++ /dev/null
@@ -1,112 +0,0 @@
----
-title: JavaScript 编程技巧
-slug: Mozilla/JavaScript_Tips
-translation_of: Mozilla/JavaScript_Tips
----
-<h2 id="Function_and_variable_naming" name="Function_and_variable_naming">XUL 技巧</h2>
-
-<ul>
- <li>在使用 XUL 覆盖层来插入代码时, 使用一个唯一的名称将函数和变量封装在对象内部,以避免与当前或未来的函数,变量名称发生冲突。</li>
-</ul>
-
-<pre class="eval">var UniqueName = {
- _privateMember: 3,
- publicMember: "A string",
-
- init: function UN_init() {
- this.doSomething(this.anotherMember);
- },
-
- doSomething: function UN_doSomething(aParam) {
- alert(aParam);
- }
-};
-</pre>
-
-<h2 id="XPConnect" name="XPConnect">XPConnect</h2>
-
-<ul>
- <li>Don't use object methods and properties more than you have to. It is often faster to store the result in a temporary variable.</li>
- <li>Don't call methods that you don't have to. For instance, <code>windowManager.getEnumerator(aType).hasMoreElements()</code> may be replaced with <code>windowManager.getMostRecentWindow(aType) != null for a single window</code>.</li>
- <li>Don't query interfaces unless you need to access methods and properties of that interface. You do not have to query interfaces to compare objects, nor to pass objects as parameters (This is different from C++, where you <em>do</em> have to query interfaces in both cases).</li>
- <li>Don't call <code>QueryInterface</code> unless you expect to succeed. Instead, use <code>instanceof</code>, e,g,:</li>
-</ul>
-
-<pre>if (target instanceof Components.interfaces.nsIRDFResource)
- return target.Value;
-if (target instanceof Components.interfaces.nsIRDFLiteral)
- return target.Value;
-return null;
-</pre>
-
-<ul>
- <li>Don't test the return value of <code>QueryInterface</code>, it always returns the original variable if it succeeds. XPConnect knows all about tearoffs and modifies the object that you <code>QueryInterface</code> or <code>instanceof</code> to cache all its known interfaces.</li>
- <li>When passing an object to an XPCOM method it is helpful if the object you pass is an XPCOM object, so that the C++ method access a C++ object. However, this is not always necessary or desirable. For instance the offline observer declared above is a JavaScript object that is registered with an XPCOM object, so that the call back from XPCOM executes the JavaScript method. Some XPCOM methods expect an object that implements several interfaces thus requiring you to write a <code>QueryInterface</code> method. However, in JavaScript this is quite simple even in the case of a weak reference which in C++ requires a helper class:</li>
-</ul>
-
-<pre>var weakObserver = {
- QueryInterface: function QueryInterface(aIID) {
- if (aIID.equals(Components.interfaces.nsIObserver) ||
- aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
- aIID.equals(Components.interfaces.nsISupports))
- return this;
- throw Components.results.NS_NOINTERFACE;
- },
- observe: function observe(aSubject, aTopic, aState) {
- }
-}
-</pre>
-
-<ul>
- <li>When declaring XPCOM methods, try to use the same names for method parameters as are used in the interface definition.</li>
-</ul>
-
-<h2 id="DOM_elements" name="DOM_elements">DOM 元素</h2>
-
-<ul>
- <li>DOM 元素只是带有一些预缓存的接口的 XPCOM 对象。</li>
- <li>不要调用 <a href="/zh-CN/docs/Web/API/Element/getAttribute">getAttribute</a> 方法来检查属性是否存在,使用<a href="/zh-CN/docs/Web/API/Element/hasAttribute"> hasAttribute</a> 代替。</li>
- <li>倾向于通过childNode 循环的方式去遍历, 而不是对 first/lastChild 使用 next/previousSibling 。 使用 hasChildNodes() 而不是 <code>childNodes.length &gt; 0</code>. 类似的,使用 <code>document.getElementsByTagName(aTag).item(0) != null</code> 而不是 <code>document.getElementsByTagName(aTag).length &gt; 0</code>.</li>
- <li>使用 localName 而不是 tagName.</li>
- <li>XUL 元素有许多特性与属性相映射。这样做是有原因的,所以要使用它们!属性有:
- <ul>
- <li>id</li>
- <li>align</li>
- <li>dir</li>
- <li>flex</li>
- <li>orient</li>
- <li>pack</li>
- <li>observes</li>
- <li>contextMenu</li>
- <li>tooltip</li>
- <li>width</li>
- <li>height</li>
- <li>minWidth</li>
- <li>minHeight</li>
- <li>maxWidth</li>
- <li>maxHeight</li>
- <li>persist</li>
- <li>left</li>
- <li>top</li>
- <li>datasources</li>
- <li>ref</li>
- <li>tooltipText</li>
- <li>statusText</li>
- <li>allowEvents</li>
- </ul>
- </li>
- <li>XUL also maps the <code>ordinal</code> attribute but this defaults to "1" if it is not present.</li>
- <li>XUL also maps the <code>class</code> attribute, but unfortunately <code>class</code> is a reserved identifier, so the property is named <code>className</code>. (The property could have been implemented as <code>{{ mediawiki.external('"class"') }}</code> but that just looks silly.)</li>
- <li>XUL also maps the <code>hidden</code> and <code>collapsed</code> attributes to properties, but note that these are boolean properties whereas the above list are all string properties.</li>
- <li>XUL also maps other useful properties and methods using XBL bindings; these vary from element to element.</li>
- <li>For best performance give ids to all important elements. However in addition to locating elements by tag name XUL also allows you to locate an element by attribute, starting at any element in the document.</li>
-</ul>
-
-<h2 id="References" name="References">References</h2>
-
-<ul>
- <li>This was started as a reprint of <a class="external" href="http://neil.rashbrook.org/JS.htm" title="http://neil.rashbrook.org/JS.htm">Neil's guide</a></li>
- <li>Some more current info on this <a class="external" href="http://autonome.wordpress.com/2006/03/24/">blog post</a></li>
-</ul>
-
-<p>{{ languages( { "ja": "ja/JavaScript_style_guide" } ) }}</p>