处理 API 设计和 OO 糖
入门阅读:
按照上述模式,我创建库/API 作为现在
var Proto = {
constructor: function () {
this.works = true;
},
method: function () {
return this.works;
}
};
,库用户要与我的原型(不提供工厂函数)进行交互,他们必须实例化并初始化对象,
// instantiate
var p = Object.create(Proto);
// initialize
p.constructor();
这是强制用户实例化和初始化我的对象的不友好且冗长的方式。
就我个人而言,因为我在所有应用程序中使用 pd
我有以下糖
// instantiate or initialize
var p = Proto.new();
// or without bolting onto Object.prototype
var p = pd.new(Proto);
但是我认为将 pd 强加给用户是不友善的,所以我不确定让我的库可用的最佳方法是什么。
- 人们创建
Proto
的新实例并自己调用.constructor
- 强制人们使用
pd
- 提供
.create
风格的工厂函数 - 放弃并使用
new
和.prototype
1 和 2 已经提到过。
3 基本上是
Proto.create = pd.new.bind(pd, Proto);
4 会让我感到难过,但确认已知的标准做事方式会增加可用性。
一般来说,当使用非标准 OO 模式时,允许人们在他们的应用程序中使用我的库的最简单的机制是什么?
我现在很想说
// 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
Introductory reading:
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.
- People create new instances of
Proto
and call.constructor
themself - Force people to use
pd
- Give
.create
style factory functions - 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,如果您使用一个小型 API 来帮助您构建传统的构造函数,使用看起来几乎像原型即类的语法,那么您可能会在库客户端上变得最简单。 API 用法示例:
实现:
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:
Implementations:
我认为正确的方法是根据“use pd”选项提供 new(Prototype, [arguments]) 函数。从依赖关系的角度来看,它甚至不应该那么糟糕(因为无论如何你都可以单独打包这个函数,并且只有几行代码)
提供一个特殊的函数也符合某种历史观点。如果你回到 Smalltalk,甚至在像 Python 这样的最近的情况下,你有单独的对象创建(new)和初始化(init,构造函数)函数,我们没有注意到这种分离的唯一原因是因为它们提供了语法用于对象实例化的糖。
I think the way to go is providing the
new(Prototype, [arguments])
function as per the "use pd" option. It should even not be that bad from a dependency point of view (since you could have packaged this function separately anyway and is has just a couple of lines of code)Offering a special function also fits in a sort of historic perspective. If you go way back to Smalltalk or even in more recent cases like Python you have separate functions for object creation (new) and initialization (init, the constructor) and the only only reason we don't notice the separation is because they provide syntactic sugar for object instantiation.