Introductory reading:
Following the patterns described above, I create libraries / APIs as follows
var Proto = { constructor: function () { this.works = true; }, method: function () { return this.works; } };
Now that library users interact with my prototypes (which do not provide a factory function), they must create and initialize an object
This is an unfriendly and verbose way to get users to create 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 find it undesirable to force pd to be used by users, so I'm not sure if the best way is to make my libraries useful.
- People create new
.constructor
instances and call the .constructor
themselves - Force
pd
- Give
.create
style to factory function - Refuse and use
new <Function>
and .prototype
1 and 2 have already been mentioned.
3 will be basically
Proto.create = pd.new.bind(pd, Proto);
4 would make me sad, but acknowledging a well-known standard way of doing things increases usability.
As a rule, when using non-standard OO templates, the simplest mechanisms allow users to use my library in their application?
I am 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