Git Product home page Git Product logo

closure-type's Introduction

closureType

closureType is a helper function for defining JavaScript object types using an enhanced version of the revealing module pattern that supports mixins.

Demo

// Create a "closure type" by invoking closureType with a callback.
// It will return to you a type factory function for creating instances.
var postType = closureType(
  // These args are passed to the main type and all function mixins.
  //
  // - `self` is an object to hold private instance state.
  // - `api` is the object returned from the type factory function, where
  //   you should attach all public methods and instance state.
  // - `initArgs` is a function that carries the arguments passed to the type
  //   factory function, taking a function for performing initialization logic.
  function(self, api, initArgs) {
    // Invoking init logic immediately.
    // You could also delay it with:
    //
    // var init = initArgs(function(title, body) {
    //   self.title = title;
    //   self.body = body;
    // });
    //
    // Then call `init()` later.
    initArgs(function(title, body) {
      self.title = title;
      self.body = body;
    })();

    // closureType.extend() is a handy helper function
    // for adding properties to the api object.
    closureType.extend(api, {
      getBody: function() {
        return self.body;
      },

      getTitle: function() {
        return self.title;
      },

      setBody: function(newBody) {
        if (!newBody) {
          return;
        }
        self.body = newBody;
        // We can use the mixed-in `emit()` method from
        // EventEmitter to notify subscribers of changes.
        api.emit('bodyChange', newBody);
      },

      setTitle: function(newTitle) {
        if (!newTitle) {
          return;
        }
        self.title = newTitle;
        api.emit('titleChange', newTitle);
      }
    });
  },
  // Mixins can be functions that take `self`, `api`, and `initArgs` or
  // objects (whose methods will be added to `api` with `this` bound to
  // `self`.
  [commentable, EventEmitter.prototype, likable]
);

// Mixins are just functions that take the closureType args.
function commentable(self, api, initArgs) {
  initArgs(function() {
    // Mixins can add instance state.
    self.comments = [];
  })();

  // Mixins can add public properties.
  closureType.extend(api, {
    addComment: function(comment) {
      self.comments.push(comment);
    },

    getComment: function(index) {
      return self.comments[index];
    },

    getComments: function() {
      return self.comments.slice();
    },

    numComments: function() {
      return self.comments.length;
    }
  });
}

function likable(self, api, initArgs) {
  initArgs(function() {
    self.numLikes = 0;
  })();

  closureType.extend(api, {
    addLike: function() {
      self.numLikes += 1;
    },

    numLikes: function() {
      return self.numLikes;
    }
  });
}

FAQ

Coming soon.

closure-type's People

Contributors

whastings avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.