With the resurgence of interest in dynamic HTML comes a proliferation of JavaScript frameworks. Some are specialized, focusing on just one or two of the above tasks. Others attempt to catalog every possible behavior and animation, and serve these all up pre-packaged. To maintain the wide range of features outlined above while remaining relatively compact, jQuery employs several strategies:
隨著對動態html內容的興趣的復甦,已經產生了很多js框架,一些專門化的關注於僅僅一兩個的以上的任務。其他企圖處理所有可能的行為和動畫,服務所有的沒有希望的簡易包裝。為了在相對簡單的同時實現上面提到的很大範圍的特點,jquery使用了以下幾個策略:
Leverage knowledge of CSS: By basing the mechanism for locating page elements on CSS selectors, jQuery inherits a terse, yet legible, way of expressing a document's structure. The jQuery library becomes an entry point for designers who want to add behaviors to their pages because a prerequisite for doing professional web development is to have knowledge of CSS syntax.
使用css的知識:通過使用css選擇器查詢元素的機制,jquery繼承了一個簡潔的清晰的表達檔案結構的方法。jquery成為了想為自己的網頁新增行為的設計師開始的點,因為做專業的wen開發的先決條件就是有css語法知識。
Support extensions: In order to avoid feature creep, jQuery relegates specialcase uses to plugins. The method for creating new plugins is simple and well-documented, which has spurred the development of a wide variety of inventive and useful modules. Most of the features in the basic jQuery download are internally realized through the plugin architecture, and can be removed if desired, yielding an even smaller library.
支援擴充套件:為了避免功能變更,jquery把特殊場景下使用的功能交由外掛實現。建立新外掛的功能很簡單而且文件寫的很好,這鼓舞了很多中的有用的模組的發展。大部分的下載下來的基礎jquery中的大部分特點在內部通過外掛機制實現,如果需要的話,可以被移除,生成一個甚至更小的js庫。
Abstract away browser quirks: An unfortunate reality of web development is that each browser has its own set of deviations from published standards. A significant portion of any web application can be relegated to handle features differently on each platform. While the ever-evolving browser landscape makes a perfectly browser-neutral code base impossible for some advanced features, jQuery adds an abstraction layerthat normalizes the common tasks, reducing the size of code while tremendously simplifying it.
解決瀏覽器相容性問題:對web開發來說,有一個不幸的事實——每一個瀏覽器都有自己一系列的與釋出的標準的偏差,對任何一個web應用來說,一個重要的部分就是處理每一個平臺的不同。然而多種瀏覽器平臺膽汁創造完美的瀏覽器相容的待吧對某些高階的功能來說使幾乎不可能的。jquery新增了一個抽象層,使這些平常的任務變的正常,在簡化的同時縮短了程式碼的大小。
Always work with sets: When we instruct jQuery, "Find all elements with the class collapsibleand hide them," there is no need to loop through each returned element. Instead, methods such as .hide()are designed to automatically work on sets of objects instead of individual ones. This technique, called implicit iteration, means that many looping constructs become unnecessary, shortening code considerably.
同時處理一系列元素:當我們命令jquery:"找到所有帶有collapsible的類,並隱藏他們",我們不需要通過每一個返回的值迴圈操作。相反,.hide()這樣的方法被設計的就是自動和一系列的物件一起工作。而不是和分離的。這種技術被稱之為隱式迭代,意味著很多迴圈可以不是必須的,優雅的減少了程式碼。
Allow multiple actions in one line: To avoid overuse of temporary variables or wasteful repetition, jQuery employs a programming pattern called chainingfor the majority of its methods. This means that the result of most operations on an object is the object itself, ready for the next action to be applied to it.
允許在一行程式碼中操縱多個動作:為了避免濫用臨時變數和浪費的重複,jquery使用了被稱之為chaining的程式設計模式作為他的方法的主體。這意味著對一個物件的大部分操作的返回值是這個物件自己,準備好了讓下一個操作去使用。
These strategies have kept the jQuery package slim—roughly 30 KB, compressed—while at the same time providing techniques to keep our custom code that uses the library compact.
這些策略讓jquery變的很輕,壓縮後大約只有30kb,同時提供技術讓我們讓使用了jquery的普通的程式碼變的簡潔。
The elegance of the library comes about partly by design, and partly due to the evolutionary process spurred by the vibrant community that has sprung up around the project. Users of jQuery gather to discuss not only the development of plugins, but also enhancements to the core library. The users and developers also assist in continually improving the official project documentation,which can be found at http://api.jquery.com.
jquery程式碼的優雅部分源於設計,部分源於受圍繞jquery如雨後春筍般建立的充滿生機的社群的鼓舞而發生的進化。jquery的使用者聚集在一起不僅僅討論開發外掛,同時討論增強核心庫的功能。使用者和開發者也幫助持續提高官方專案的文件,你可以在 http://api.jquery.com上找到這些文件。
Despite all of the efforts required to engineer such a flexible and robust system, the end product is free for all to use. This open source project is dually licensed under the MIT License(to permit free use of jQuery on any site and facilitate its use within proprietary software) and the GNU Public License(appropriate for inclusion in other GNU-licensed open-source projects).
儘管付出了很大的努力去開發這樣靈活而且健壯的系統,最終的產品依然是對所有人免費的。這個開源專案在MIT許可(允許在任何站點上免費使用jquery,同時在正確的軟體上正確的促進他的使用)和GNU公共許可(包含其他GNU許可的開源專案是可以的)的雙重許下。