简体   繁体   English

Mootools类中的私有方法

[英]Private Methods in a Mootools Class

I'm relatively new to using oop in Javascript, and I'm wondering what the best practice is for private methods. 我在Javascript中使用oop相对较新,我想知道私有方法的最佳实践是什么。 Right now, I'm using mootools to create my classes and I'm simulating private methods by prefixing them with an underscore and forcing myself not to call the method outside of the class. 现在,我正在使用mootools来创建我的类,我通过在前面添加下划线来强制私有方法,并强迫自己不要在类之外调用该方法。 So my class looks like: 所以我的班级看起来像:

var Notifier = new Class(
{
   ...
   showMessage: function(message) { // public method
      ...
   },

   _setElementClass: function(class) { // private method
      ...
  }
});

Is this a good/standard way to handle private methods in JS? 这是在JS中处理私有方法的好/标准方法吗?

MooTools provides a protect method on functions, so you can call protect on any method that you want to protect from being called outside the Class . MooTools为函数提供了一种protect方法,因此您可以在要保护的任何方法上调用protect来防止在Class外部调用。 So you can do: 所以你可以这样做:

​var Notifier = new Class({
    showMessage: function(message) {

    },
    setElementClass: function(klass) {

    }.protect()
})​;

var notifier = new Notifier();
notifier.showMessage();
notifier.setElementClass();
> Uncaught Error: The method "setElementClass" cannot be called.

Not that class is a future reserved keyword in JavaScript and your code may break when using it. 并非class是JavaScript中未来的保留关键字,您的代码在使用时可能会中断。 It certainly breaks on Safari at this point, but the behavior in other browsers is not guaranteed as well, so it's better to not use class as an identifier at all. 在这一点上肯定会破坏Safari,但是其他浏览器中的行为也不能得到保证,因此最好不要使用class作为标识符。

One advantage of using protect over creating closures yourselves is that if you extend this class, you can still access the protected methods in subclasses. 使用protect不是创建闭包的一个优点是,如果扩展此类,您仍然可以访问子类中的受保护方法。

Notifier.Email = new Class({
    Extends: Notifier,

    sendEmail: function(recipient, message) {
        // can call the protected method from inside the extended class
        this.setElementClass('someClass');
    }
});

var emailNotifier = new Notifier.Email();
emailNotifier.sendEmail("a", "b");
emailNotofier.setElementClass("someClass");
> Uncaught Error: The method "setElementClass" cannot be called.

If you want to use a naming convention such as prefixing or suffixing _ before or after a method, then that's perfectly fine as well. 如果你想在方法之前或之后使用命名约定,例如前缀或后缀_ ,那么这也很好。 Or you can combine the _ with the protected methods too. 或者您也可以将_与受保护的方法结合起来。

Well, as long as you stay consistent you won't get into trouble. 好吧,只要你保持一致,你就不会遇到麻烦。

There is a pattern though, for creating true privacy in javascript via closure . 但是,有一种模式可以通过关闭在javascript中创建真正的隐私。

var Notifier = function() {

    // private method
    function setElementClass(class) { 
        //...
    }

    // public method
    this.showMessage = function(message) {
        // ...
        setElementClass(...) // makes sense here
    };
};

var noti = new Notifier();
noti.showMessage("something");     // runs just fine
noti.setElementClass("smth else"); // ERROR: there isn't such a method

If you want to add public methods that are inherited and shared between all objects (smaller memory footprint), you should add them to the object's prototype. 如果要添加在所有对象之间继承和共享的公共方法(较小的内存占用量),则应将它们添加到对象的原型中。

// another way to define public functions
// only one will be created for the object
// instances share this function
// it can also be used by child objects
// the current instance is accessed via 'this'
Notifier.prototype.showMessage = function() {
   // ...
   this.otherPublicFunction(...);
};​

I recommend you to look into the raw way of handling objects in javascript, because only then you will be able to know what you're doing. 我建议你研究在javascript中处理对象的原始方式,因为只有这样你才能知道你在做什么。 Mootools like classes would be good to hide what this language differs from others. 像课程一样的Mootools可以很好地隐藏这种语言与其他语言的不同之处。 But truth is that it differs so greatly that it would be naive to think that you do the same thing when you say class in javascript, like in any other class-based OO language. 但事实是它的差异如此之大,以至于当你在javascript中说class时,如同在任何其他基于类的OO语言中一样,认为你做同样的事情是天真的。

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

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