简体   繁体   English

Javascript私有方法和私有类变量

[英]Javascript private methods and private class variables

I'm not a professional Javascript dev, so my experience is limited to personal projects using tutorials online as well as trial and error. 我不是专业的Javascript开发人员,所以我的经验仅限于使用在线教程以及反复试验的个人项目。 I've just recently had the need to create an object that has its own methods and member variables etc. I've been reading Doug Crockford notes and others stuff but I'm struggling to understand how it all fits together. 最近,我刚刚需要创建一个具有自己的方法和成员变量等的对象。我一直在阅读Doug Crockford的笔记和其他内容,但我一直在努力理解它们如何组合在一起。

Consider the following code: 考虑以下代码:

var CLASS_VAR = "some_val";

function MyClass(opts) {
    this.options = opts;
    this.myContainer = null;
}

MyClass.prototype.doIt = function() {

    var privateMethod = function(options) {
        var container = document.createElement("div");
        // use CLASS_VAR and 'options' in some way
        return container;
    }

    this.myContainer = privateMethod(this.options);
}

This is essentially the pattern that a portion of my code is using, but I just don't know if it's efficient or proper. 这实质上是我的代码的一部分正在使用的模式,但是我只是不知道它是否有效或适当。 Is this pattern a good idea, or are there more sensible/efficient/correct ways of achieving the same thing? 这种模式是一个好主意,还是有更明智/有效/正确的方式来实现同一目标? In my actual code, there is more than one private function in doIt . 在我的实际代码中, doIt有多个私有函数。 Also, there is ~50 "class" variables in the actual code, so should I keep these in the global scope, or is there some way to make them private class variables as in Java private static final ? 另外,实际代码中有〜50个“类”变量,因此我应该将它们保留在全局范围内,还是像Java private static final一样,有某种方法使它们成为私有类变量?

Edited to reflect first comment 编辑以反映第一条评论

"@plalx well that's really what I was asking, I suppose. You see, I actually have 4 or 5 functions inside doIt (obviously its not called that) and those functions are a lot longer than the example. If I was to extract them all and dump everything into doIt it would be a bit of a mess." “ @plalx很好,我想这就是我要问的。您知道,我实际上在doIt内部有4或5个函数(显然它没有这样称呼),而且这些函数比示例更长。如果我要提取它们,一切都丢进去做,那会有点混乱。”

Well, if the functions are only meant to be used inside doIt , you could do something like: 好吧,如果这些功能仅打算在doIt内部使用,则可以执行以下操作:

MyClass.prototype.doIt = (function() {

    function privateMethod(options) {
        var container = document.createElement("div");
        // use CLASS_VAR and 'options' in some way
        return container;
    }

    function someOtherPrivateMethod() {}

    return function () {
        this.myContainer = privateMethod(this.options);
    };
})();

In the example you provided, there aren't any 'real' private methods. 在您提供的示例中,没有任何“真正的”私有方法。 The method 'privateMethod' is just a standard member of the 'doIt' method. 方法“ privateMethod”只是“ doIt”方法的标准成员。

If you need to create real private members - The following is an excellent guide on Javascript Patterns (I recommend you start with the 'Revealing' pattern, and take it from there): Javascript Design Patterns 如果您需要创建真正的私有成员-以下是有关Javascript模式的出色指南(我建议您从“公开”模式开始,然后从那里开始使用): Javascript设计模式

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

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