Exploring introductory materials:
- View on Prototypes as "classes"
- Insights into OO JS
Implementing libraries/APIs following the mentioned patterns leads to the creation of objects like this:
var Proto = {
constructor: function () {
this.works = true;
},
method: function () {
return this.works;
}
};
However, requiring library users to manually instantiate and initialize objects might not be user-friendly.
Personally, with my preference for pd
, I have come up with a more concise approach:
// Simplified instantiation and initialization
var p = Proto.new();
// Alternatively, without extending Object.prototype
var p = pd.new(Proto);
Yet, it feels unfair to enforce the use of pd
on all users. Hence, I am uncertain about the best way to enhance usability of my libraries.
- Users manually create instances of
Proto
and call.constructor
- Mandatory usage of
pd
- Implementation of factory functions using
.create
- Considering standard
new <Function>
and.prototype
method
Options 1 and 2 have already been discussed.
Option 3 involves:
Proto.create = pd.new.bind(pd, Proto);
Although option 4 goes against my current approach, adhering to established conventions can improve overall usability.
When adopting non-standard OO practices, what approaches can be employed to enable seamless integration of my library into diverse applications?
At present, I am leaning towards:
// Prototype definition
Proto;
// Instantiation process
var p = Object.create(Proto);
// Initialization step
p.constructor();
// Utilize pd.new for added simplicity