Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用原型&x27;s Class.create来定义私有/受保护的属性和方法_Javascript_Oop_Prototypejs_Private_Protection - Fatal编程技术网

Javascript 使用原型&x27;s Class.create来定义私有/受保护的属性和方法

Javascript 使用原型&x27;s Class.create来定义私有/受保护的属性和方法,javascript,oop,prototypejs,private,protection,Javascript,Oop,Prototypejs,Private,Protection,有一种很好的通用方法,可以在Javascript中定义私有和受保护的属性和方法。然而,Prototype的当前版本(1.6.0)没有通过其语法来定义它们的内置方式 我很好奇,当开发人员在使用Prototype时想要定义私有和受保护的属性和方法时,最佳实践是什么。有没有比通用方法更好的方法?在Prototype's lighthouse中有一个讨论,解释了为什么Prototype的Class.create不能获得这种效果。你可以做的是在构造函数中使用局部变量(初始化)然后创建一个闭包,该闭包将访问

有一种很好的通用方法,可以在Javascript中定义私有和受保护的属性和方法。然而,Prototype的当前版本(1.6.0)没有通过其语法来定义它们的内置方式


我很好奇,当开发人员在使用Prototype时想要定义私有和受保护的属性和方法时,最佳实践是什么。有没有比通用方法更好的方法?

在Prototype's lighthouse中有一个讨论,解释了为什么Prototype的Class.create不能获得这种效果。

你可以做的是在构造函数中使用局部变量(初始化)然后创建一个闭包,该闭包将访问/向公共方法公开此变量

下面是一个代码示例:

// properties are directly passed to `create` method
var Person = Class.create({
   initialize: function(name) {
      // Protected variables
      var _myProtectedMember = 'just a test';

      this.getProtectedMember = function() {
         return _myProtectedMember;
      }

      this.name = name;
   },
   say: function(message) {
      return this.name + ': ' + message + this.getProtectedMember();
   }
});
这是道格拉斯·克罗克福德关于这个问题的理论


关键是将公共方法添加为闭包,如下例所示:

 Bird = Class.create (Abstract,(function () {
    var string = "...and I have wings"; //private instance member
    var secret = function () {
        return string;
    } //private instance method
    return {
        initialize: function (name) {
            this.name = name;
        }, //constructor method
        say: function (message) {
            return this.name + " says: " + message + secret();
        } //public method
    }
})());

Owl = Class.create (Bird, {
    say: function ($super, message) {
        return $super(message) + "...tweet";
    } //public method
})

var bird = new Bird("Robin"); //instantiate
console.log(bird.say("tweet")); //public method call

var owl = new Owl("Barnie"); //instantiate
console.log(owl.say("hoot")); //public method call inherit & add

令人惊叹的。谢谢我也对TML的答案投了赞成票,因为他包含的链接对此主题进行了全面讨论,并揭示了为什么其他(看起来更干净的)实现不起作用。@SleepyCod:您如何定义一个完全封装的私有变量,即没有setter或getter的变量?您的示例有一个getter,其他类方法可以获取该字段,但这会将该字段暴露给外部世界。@Christophe Eblé这不是私有方法。使用变量getProtectedMember=function(){…但是,从其他方法中访问此方法是不可能的,但至少不能从类外访问。我正在研究是否可以允许从内部而不是外部访问某个方法…你能给我访问此链接的权限吗?我同意你的意见,但我希望看到讨论,但它需要登录。它不允许我使用我的谷歌登录…是一个链接到archive.org捕获的讨论。谢谢。这是一个非常有趣的讨论。我很想看到它实现。我已经详细查看了补丁,它看起来非常可靠。我将对合并到prototype-1.7.3.j中的补丁进行一些测试看看这是否可靠。。。