Javascript:我应该隐藏我的实现吗?
作为一名 C# 程序员,我有一个习惯,将可以而且应该私有的东西设为私有,当 JS 类型向我公开其所有私有部分时,我总是有一种奇怪的感觉(而且这种感觉并没有被“唤起”) )。假设我有一个具有 draw
方法的类型,该方法在内部调用 drawBackground
和 drawForeground
,而单独调用它们是没有意义的。我应该如何实施这个?
选项 1
Foo = function(){
this.draw();
};
Foo.prototype.draw = function(){
this.drawBackground();
this.drawForeground();
};
Foo.prototype.drawBackground = function(){};
Foo.prototype.drawForeground = function(){};
选项 2
Foo = (function(){
var constructor = function(){
this.draw();
};
var drawBackground = function(){};
var drawForeground = function(){};
constructor.prototype.draw = function(){
drawBackground.call(this);
drawForeground.call(this);
};
return constructor;
})();
当然,区别在于第一个示例中的 drawBackground
和 drawForeground
方法是公共 API 的一部分,而它们在第二个 API 中对外部隐藏。这是可取的吗?我应该更喜欢哪一个?我将 C# 习惯应用于 Javascript 是错误的吗?我应该让 Javascript 中的所有内容都可扩展和可重写吗? .call(this)
对性能有何影响?
As a C# programmer, I have a bit of a habit of making things private that can and should be private, and I always get a weird feeling when a JS type exposes all its private parts to me (and that feeling is not 'aroused'). Say I have a type that has a draw
method, which internally calls drawBackground
and drawForeground
, which make no sense to be called on their own. How should I implement this?
Option 1
Foo = function(){
this.draw();
};
Foo.prototype.draw = function(){
this.drawBackground();
this.drawForeground();
};
Foo.prototype.drawBackground = function(){};
Foo.prototype.drawForeground = function(){};
Option 2
Foo = (function(){
var constructor = function(){
this.draw();
};
var drawBackground = function(){};
var drawForeground = function(){};
constructor.prototype.draw = function(){
drawBackground.call(this);
drawForeground.call(this);
};
return constructor;
})();
The difference, of course, being that in the first example, the drawBackground
and drawForeground
methods are part of the public API, while they are hidden to the outside in the second one. Is that desirable? Which one should I prefer? Am I wrong to apply my C# habits to Javascript and should I make everything extensible and override-able in Javascript? And what are the performance implications of the .call(this)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Perl 开发人员中有一句广为人知的名言,出自著名的 Camel 书:“Perl 模块宁愿你远离它的客厅,因为你没有被邀请,而不是因为它有一把猎枪。”。其理念是,如果您作为库的开发人员想要区分公共 API 和私有 API,那就太好了。这样做并记录下来。代码的调用者应该知道哪个是哪个,但如果他们决定调用您认为他们不应该调用的东西,也可以自由地表现得像个白痴。从面向对象的背景来看,这是异端邪说,但对于脚本语言来说,这就是它们的运作方式。
这个问题的答案有点主观,但我会告诉你,当我编写 JavaScript 并拥有私有的方法或变量(如果我在 .NET 中进行编码)时,我只需在它们前面加上“prv_”或“prv_”之类的前缀。 “p_”或只是“_”...无论你的船漂浮什么。这样,您就告诉您的用户,这些内容是私有的,并且可以在他们的控制下进行更改。这样,如果他们无论如何都选择调用您的私有方法,那么可疑的代码就会像一个酸痛的拇指一样突出。
There's a well known quote amongst Perl developers that comes from the (in)famous Camel book: "A Perl module would prefer that you stay out of its living room because you weren't invited, not because it has a shotgun.". The philosophy being, if you as the developer of a library want to distinguish between your public and private API, that's great. Do that and document it. The caller of your code should know which is which, but also be free to act like an idiot if they decide to and call things you don't think they should call. From an OO background, that's heretical, but with scripting languages, that's how they roll.
The answer to this is a bit subjective, but I'll tell you that when I'm writing JavaScript and have methods or variables that would be private if I were coding in .NET, I just prefix them with something like "prv_" or "p_" or just "_"... whatever floats your boat. That way, you've told your users that this stuff is meant to be private and could change out from under them. And that way, if they choose to call your private methods anyway, that iffy code will stick out like a sore thumb.