Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling API design and OO sugar

Tags:

javascript

api

Introductory reading:

  • Prototypes as "classes"
  • OO JS

Following the patterns described above I create libraries/APIs as the following

var Proto = {
  constructor: function () {
    this.works = true;
  },
  method: function () {
    return this.works;
  }
};

Now for library users to interact with my prototypes (which do not supply factory functions) they have to instantiate and initialize the object

// instantiate
var p = Object.create(Proto);
// initialize
p.constructor();

This is an unfriendly and verbose way of forcing users to instantiate and initialize my objects.

personally since I use pd in all my applications I have the following sugar

// instantiate or initialize
var p = Proto.new();
// or without bolting onto Object.prototype
var p = pd.new(Proto);

However I think it's unkind to force pd onto users so I'm not sure what's the best way to make my libraries usable.

  1. People create new instances of Proto and call .constructor themself
  2. Force people to use pd
  3. Give .create style factory functions
  4. Give up and use new <Function> and .prototype

1 and 2 have already been mentioned.

3 would basically be

Proto.create = pd.new.bind(pd, Proto);

4 would make me sad but confirming to a known standard way of doing things increases usability.

Generally when using non-standard OO patterns what are the easiest mechanisms to allow people to use my library in their application?

I'm currently tempted to say

// here is my Prototype
Proto;
// here is how you instantiate a new instance
var p = Object.create(Proto);
// here is how you initialize it
// yes instantiation and initialization are seperate and should be.
p.constructor();
// Want sugar, use pd.new
like image 353
Raynos Avatar asked Oct 20 '11 11:10

Raynos


People also ask

What are the 3 types of APIs?

There are also three common types of API architectures: REST, a collection of guidelines for lightweight, scalable web APIs. SOAP, a stricter protocol for more secure APIs. RPC, a protocol for invoking processes that can be written with XML (XML-RPC) or JSON (JSON-RPC).


1 Answers

For now, you probably make it easiest on your library clients if you use a small API that helps you with building a traditional constructor function, using syntax that looks almost like prototypes-as-classes. Example API usage:

// Superclass
var Person = Class.extend({
    constructor: function (name) {
        this.name = name;
    },
    describe: function() {
        return "Person called "+this.name;
    }
});

// Subclass
var Worker = Person.extend({
    constructor: function (name, title) {
        Worker.super.constructor.call(this, name);
        this.title = title;
    },
    describe: function () {
        return Worker.super.describe.call(this)+" ("+this.title+")";
    }
});
var jane = new Worker("Jane", "CTO");

Implementations:

  • Simple JavaScript Inheritance
  • I’ve reimplemented Resig’s API, in a way that is possibly easier to understand: rauschma/class-js
like image 76
Axel Rauschmayer Avatar answered Oct 04 '22 21:10

Axel Rauschmayer