对象的创建和继承JavaScript库:Protomatter.js

jopen 9年前

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.

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() {      // ...    }  });

项目主页:http://www.open-open.com/lib/view/home/1423723797451