對象的創建和繼承JavaScript庫:Protomatter.js
Protomatter是一個對象的創建和繼承JavaScript庫,配有私有實例屬性和私有方法。
特性
- All properties assigned tothisare private by default.
- Private methods are only accessible from the object's own methods.
- Easy creation of prototype chains (for prototypal inheritance).
- Easy invocation of overridden methods higher up in the prototype chain.
- Easy use of concatenative (multiple) inheritance with prototype composition.
- Flexible management of public methods through late-binding.
- Extension of objects post-instantiation through mixin support.
- Works in Node.js and browsers. </ul>
Creating a Prototype
UseProtomatter.create()to set up a new prototype, passing it an object with the properties you want available to instances created from the prototype. Add aninitmethod to set up any instance variables when your prototype creates an instance. Place any methods that you want to be private under the keyprivate. All methods outsideprivatewill be public.
var Modal = Protomatter.create({ init: function(title, body, triggerEl) { this.title = title; this.body = body; this.triggerEl = triggerEl; this.attachEventListeners(); }, getBody: function() { return this.body; }, getTitle: function() { return this.title; }, hide: function() { this.removeBackdrop(); // ... }, private: { attachEventListeners: function() { // No need to bind this.show, as Protomatter will set context correctly. this.triggerEl.addEventListener('click', this.show); }, removeBackdrop: function() { // ... } }, show: function() { // ... } });
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!