在构造函数中向原型添加属性
我正在尝试一些示例,并遇到一个问题,如果我们想向原型添加一个函数,它将无法访问构造函数的私有成员。我遇到了这个解决方案。这似乎是一个不错的技巧。
我尝试了其他一些方法,得到了以下结果:
var Restaurant = function()
{
var myPrivateVar;
var private_stuff = function() // Only visible inside Restaurant()
{
return "I can set this here!";
}
Restaurant.prototype.use_restroom = function() // use_restroom is visible to all
{
private_stuff();
}
Restaurant.prototype.buy_food = function() // buy_food is visible to all
{
return private_stuff();
}
}
var restaurant = new Restaurant();
restaurant.buy_food(); // this would work
restaurant.private_stuff(); // this won't
该解决方案看起来很奇怪,因为我们在构造函数中添加到原型中。 (我没见过太多这样的东西)。它至少适用于 firefox 5 和 chrome。难道是有什么问题吗?
I was experimenting with some examples and came across a problem that if we want to add a function to a prototype it will not be able to access the private members of the constructor. I came across this solution. This seems to be a nice hack.
I tried out some other ways and I got the following:
var Restaurant = function()
{
var myPrivateVar;
var private_stuff = function() // Only visible inside Restaurant()
{
return "I can set this here!";
}
Restaurant.prototype.use_restroom = function() // use_restroom is visible to all
{
private_stuff();
}
Restaurant.prototype.buy_food = function() // buy_food is visible to all
{
return private_stuff();
}
}
var restaurant = new Restaurant();
restaurant.buy_food(); // this would work
restaurant.private_stuff(); // this won't
The solution seems weird because we are adding to the prototype within the constructor function. (I haven't seen much of this). It works on firefox 5 and chrome at least. Is there something wrong with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您所做的就是每次创建新的餐厅对象时在原型上重新定义这些方法。更明智的方法是在
this
上定义它们,这是在构造函数中构造的新对象:不过,您可以这样做,而不是使用
new< /code>:
然后执行:
这称为揭示模块模式。缺点是每个新对象都会获得所有函数的副本,如果您在构造函数中向
this
添加方法,也会发生这种情况。揭示模块模式的一个非常小的替代版本(我认为读起来更好一点)如下所示:
然后,如果您想更改函数是否是私有的,只需在返回的函数中添加或删除它即可目的。
What you're doing is redefining those methods on the prototype every time you make a new restaurant object. The more sane way to do that would be to define them on
this
, which is the new object being constructed in a constructor:You could just do it like this though, and not use
new
:and then just do:
This is called the revealing module pattern. The downside is that each new object gets a copy of all the functions, which also happens if you add methods to
this
in your constructor.A very small alternative version of the revealing module pattern (which I think reads a bit better) looks like this:
Then, if you want to change whether a function is private or not, it's just a matter of adding or removing it from the returned object.
我实际上没有对此进行测试,但我认为所有对象都会访问最后实例化对象的私有属性。
在每个实例化中,您将原型方法(在所有实例之间共享)绑定到正在实例化的对象的私有变量:)
I didn't actually test this, but I think all the objects would access to the last instantiated object's private properties.
On each instantiation you're binding the prototype methods (shared across all instances) to the private variables of the object being instantiated :)
老实说,这对我来说没有多大意义。当然,您可以通过这种方式调用您的私有函数,但这并不能解决最初的问题 - 也就是说,您仍然需要在构造函数中添加方法。
如果你想在构造函数之外向类添加方法,你可以使用闭包来保持构造函数的干净:
Honestly, it doesn't make a lot of sense to me. Sure, you can have calls to your private functions this way, but it doesn't solve the initial problem - that is, you still need to add methods inside the constructor.
If you want to add methods to the class outside the constructor, you can use closures to keep constructors clean:
我们采取不同的方法。我们有时确实会使用闭包,但仅当您需要在类级别管理状态时才使用。我们使用命名空间来管理范围。对于具有原型方法的简单类,我们只需这样做:
We take a different approach. We do use closures sometimes, but only when you need to manage state at the class level. We use namespaces to manage scope. For a simple class with prototype methods, we just do this: