基于闭包的对象的效率问题
在 Javascript 中定义类的一种方法是使用闭包和对象字面量。作为一个例子,考虑一个圆:
var circle = function(radius) {
var radius = radius;
return {
area: function() {
return Math.PI*(radius*radius);
}
}
}
然后可以用保留封装的内部变量来实例化圆对象:
c = circle(1);
c.area(); // Returns pi
c.radius; // Returns undefined
不幸的是,使用这种方法,如果我要创建多个圆,则将为每个实例复制面积函数。这是通常使用原型的地方:
var Circle = function(radius) {
this.radius = radius;
}
Circle.prototype.area = function() {
return Math.PI*(this.radius*this.radius);
};
现在的问题是变量不能声明为私有:
c = new Circle(1);
c.area(); // Returns pi
c.radius; // Returns 1
有谁知道我如何充分利用两者?效率是一个问题,所以宁愿避免第一种方法的当前表述,而且我正在为公共 API 编写,因此不希望用户访问内部变量/方法。提前致谢...
One way to define a class in Javascript is with closures and the object literal. As an example, consider a circle:
var circle = function(radius) {
var radius = radius;
return {
area: function() {
return Math.PI*(radius*radius);
}
}
}
Circle objects can then be instantiated with the internal variables remaining encapsulated:
c = circle(1);
c.area(); // Returns pi
c.radius; // Returns undefined
Unfortunately with this method, if I was to create multiple circles the area function would be copied for each instance. This is where the prototyping is usually used:
var Circle = function(radius) {
this.radius = radius;
}
Circle.prototype.area = function() {
return Math.PI*(this.radius*this.radius);
};
Now the problem is that variables cannot be declared private:
c = new Circle(1);
c.area(); // Returns pi
c.radius; // Returns 1
Does anyone know how I would get the best of both? Efficiency is an issue so would rather avoid the current formulation of the first method, and I am writing for a public API so don't want users to have access to internal variables/methods. Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
现在
Circle
伪类的每个实例都有两个方法:area
,它是从原型继承的,因此使其成为公共方法getRadius
code> 被分配给实例对象,从而使其成为实例方法。不过,radius 值仍然可以通过 getRadius 检索。但是,如果
getRadius
可以确定它是否是从Circle.prototype
方法中调用的,并且仅返回radius
值(如果是),那么就会解决它,但我不确定是否可以做到。How about this:
Now each instance of the
Circle
pseudo-class has two methods:area
which is inherited from the prototype thus making it a public methodgetRadius
which is assigned to the instance object thus making it an instance methodThe
radius
value is still retrievable viagetRadius
though. However, ifgetRadius
could determine whether it's called from within aCircle.prototype
method and only return theradius
value if it is, then that would solve it, but I'm not sure that that can be done.这就是我想到的,尽管我怀疑每次创建 GetArea() 方法是否“高效”,但这是我知道从原型方法访问局部范围变量的唯一方法:(
这将允许访问到圆的半径值,但不允许您更改它,这就是为什么我认为您想要这个)
This is what I came up with, though whether creating the GetArea() method everytime is "efficient" I doubt, but it's the only way I'm aware of to access the a local scope variable from prototype methods:
(This will allow access to the value of the Radius of the circle, but not allow you to change it, which is why I presume you wanted this)