简体   繁体   English

如何使用定义的变量和函数创建JavaScript对象?

[英]How do I create a JavaScript Object with defined variables and functions?

Let's say I want to create an Object called 'Vertex'. 假设我要创建一个名为“顶点”的对象。 Usually, in Java I would do this by: 通常,在Java中,我可以通过以下方式做到这一点:

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? 现在,我将如何在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. 到目前为止,这是我设置的,但是我认为这是不对的,并且我从未使用过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);
    }
};

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. 如果不是,我基本上是在尝试通过JavaScript在上面的Java代码中完成操作。

http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/ explains it in excruciating detail. http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/对此进行了详尽的解释。 In your case... 就你而言...

// Constructor
function Vertex (data) {
    // Private
    var id = 1;

    // Privileged
    this.getID= function () {
        return id;
    };

    // Public
    this.data = data;
}

// Public
Vertex.prototype.getData = function () {
    return this.data;
};

// Static property
Vertex.static = "something";

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. 通过JavaScript奇妙的原型实现,有很多很多方法可以使类/实例系统类似于您在其他语言中使用的系统。 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的特权模型引入JavaScript几乎无济于事。

Java needs private members to be enforced as part of its security model. Java需要强制私有成员作为其安全模型的一部分。 But JavaScript has no model for in-page security boundaries, so you're not protecting your code against misuse by anyone but yourself. 但是JavaScript没有针对页内安全性边界的模型,因此您无法保护自己的代码不受他人滥用。 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). 考虑改为使用荣誉系统模型(例如在Python中)。 Prefix 'pseudo-private' members with an underscore to signal that those members should not be accessed from the outside. 在“ pseudo-private”成员之前加上下划线,以表示不应从外部访问这些成员。

You could use JSDoc to add meta information (like private membership) to your variables and functions. 您可以使用JSDoc将元信息(例如私有成员身份)添加到变量和函数。 Tools like the Google closure compiler can use this meta information to check your code for illegal access to these members. 诸如Google Closure编译器之类的工具可以使用此元信息来检查您的代码是否有对这些成员的非法访问。 (and many other things as well) (以及其他很多东西)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM