如何创建具有已定义变量和函数的 JavaScript 对象?
假设我想创建一个名为“Vertex”的对象。通常,在 Java 中我会通过以下方式执行此操作:
public class Vertex {
// member variables
public data;
private id;
// member methods
public Vertex() { /* default constructor */ }
public getID() { return id; }
}
现在,我将如何在 JavaScript 中执行此操作?我想保留私人与公共?这是我到目前为止所设置的,但我认为这是不对的,而且我从未处理过 JavaScript 中的 OOP。
/**
* Vertex constructor.
*/
function Vertex(object) {
data = object;
id = object.getCode(); // each Vertex will have a unique id which will be the city code
};
Vertex.prototype = (function() {
// Private Members here
// Public Members inside return
return {
constructor : Vertex,
getID : function() {
return (id);
}
};
我对原型一点也不熟悉,但我正在努力学习。这是正确的方法吗?如果不是,我基本上是在尝试完成上述 Java 代码的功能,但是是在 JavaScript 中完成的。
Let's say I want to create an Object called 'Vertex'. Usually, in Java I would do this by:
public class Vertex {
// member variables
public data;
private id;
// member methods
public Vertex() { /* default constructor */ }
public getID() { return id; }
}
Now, how would I do that in JavaScript? I want to preserve private vs. public? This is what I have set up so far, but I don't think it is right, and I've never dealt with OOP in JavaScript.
/**
* Vertex constructor.
*/
function Vertex(object) {
data = object;
id = object.getCode(); // each Vertex will have a unique id which will be the city code
};
Vertex.prototype = (function() {
// Private Members here
// Public Members inside return
return {
constructor : Vertex,
getID : function() {
return (id);
}
};
I'm not familiar at all with prototypes, but I'm trying to learn. Is this the right way to do this? If it isn't, I'm basically trying to accomplish what the above Java code does, but by doing it in JavaScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/ 解释得很痛苦细节。就你而言...
http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/ explains it in excruciating detail. In your case...
有很多很多方法可以通过 JavaScript 奇怪的原型实现来创建类似于您在其他语言中使用的类/实例系统。没有一种方法是每个人都可以接受的,而且在便利性和性能方面存在许多可能的权衡。有关不同公共类的讨论,请参阅此问题 /实例模型。
虽然您可以通过使用闭包来重现(大部分)私有变量,但我强烈建议不要这样做。将 Java 的权限模型引入 JavaScript 不会给您带来什么好处。
Java 需要强制执行私有成员作为其安全模型的一部分。但 JavaScript 没有页内安全边界模型,因此您无法保护您的代码免遭除您之外的任何人的滥用。所有“真正的私有”都会为您做的就是让快速调试和原型设计工作变得更加困难。
考虑使用荣誉系统模型(例如Python)。在“伪私有”成员前面加上下划线,表示这些成员不应从外部访问。
There are many, many ways to make a class/instance system similar to what you're using to in other languages, out of JavaScript's curious implementation of prototypes. There's no one single accepted way that everyone uses, and lots of possible trade-offs for convenience and performance. See this question for a discussion of different common class/instance models.
Whilst you can reproduce (mostly-)private variables by using a closure, I would strongly advise against it. You've little to gain by bringing Java's model of privileges to JavaScript.
Java needs private members to be enforced as part of its security model. But JavaScript has no model for in-page security boundaries, so you're not protecting your code against misuse by anyone but yourself. All ‘real privates’ will do for you is make rapid debugging and prototyping work a bit harder.
Consider instead using the honour system model (as in eg Python). Prefix ‘pseudo-private’ members with an underscore to signal that those members should not be accessed from the outside.
您可以使用 JSDoc 将元信息(例如私有成员身份)添加到变量和函数中。 Googleclosure 编译器等工具可以使用此元信息来检查您的代码是否存在对这些成员的非法访问。 (还有许多其他事情)
You could use JSDoc to add meta information (like private membership) to your variables and functions. Tools like the Google closure compiler can use this meta information to check your code for illegal access to these members. (and many other things as well)