5

在过去的几天里,我一直在研究一种在 MooTools 类中拥有私有或受保护属性的方法。各种文章(即 Sean McArthur 的在 MooTools 类中获取私有变量)提供了一种用于 MooTools 已弃用版本的方法,但我无法找到 MooTools 1.3+ 的工作方法。

今天,在玩了几个小时的代码之后,我我已经创建了一个合适的解决方案。我说“想想”,因为我真的没有作为程序员的经验。我希望这里的社区可以检查我的代码并告诉我它是否真的是一个有效的解决方案,或者一个 hackjob 仿真。

var TestObj = (function() {

 var _privateStaticFunction = function() { }

 return new Class({

  /* closure */
  _privates: (function() {

   return function(key, val) {

    if (typeof(this._data) == 'undefined') this._data = {};

    /* if no key specified, return */
    if (typeof(key) == 'undefined') return;

    /* if no value specified, return _data[key] */
    else if (typeof(val) == 'undefined') {
      if (typeof(this._data[key]) != 'undefined') return this._data[key];
      else return;
    }

    /* if second argument, set _data[key] = value */
    else this._data[key] = val;

   }

  /* tell mootools to hide function */
  })().protect(),

  initialize: function() {},

  get: function(val) { return this._privates(val); },
  set: function(key,val) { this._privates(key,val); }

 })

})();


obj1 = new TestObj();
obj2 = new TestObj();

obj1.set('theseShoes','rule');
obj2.set('theseShoes','suck');

obj1.get('theseShoes') // rule
obj2.get('theseShoes') // suck
obj1._privates('theseShoes') // Error: The method "_privates" cannot be called
obj1._privates._data // undefined
obj1._privates.$constructor._data // undefined

我真的很感激任何提示!感谢大家!

编辑:嗯,这很尴尬。我忘了检查显而易见的 obj1._data。我不认为this会引用实例对象!所以,我很烂。不过,任何想法都会很棒!

4

1 回答 1

4

呵呵。在你的情况下,一个更简单的模式就可以了。

考虑闭包后面的 var - 极难刺穿。它可以通过 getter 和 setter 获得。

缺点:数据值不能在实例中,或者可以直接访问。

var testObj = (function() {
    var data = {__proto__:null}; // 100% private

    return new Class({
        get: function(key) {
            return data[this.uid][key] || null;
        },
        set: function(key, value) {
            data[this.uid][key] = value;
        },
        remove: function(key) {
            delete data[this.uid][key];
        },
        otherMethod: function() {
            alert(this.get("foo"));
        },
        initialize: function() {
            this.uid = String.uniqueID();
            data[this.uid] = {};
        }
    });
})(); // why exec it?


var foo = new testObj();
var bar = new testObj();

foo.set("bar", "banana");
console.log(foo.get("bar")); // banana!
console.log(bar.get("bar")); // undefined.
bar.set("bar", "apple");
console.info(foo.get("bar"), bar.get("bar")); // banana apple

在行动:http: //jsfiddle.net/dimitar/dCqR7/1/

我正在努力寻找一种方法来彻底打破这种模式——这有时可以通过这样的原型设计来实现

事实上,我玩了一些,这是没有命名空间的固定模式:

http://jsfiddle.net/dimitar/dCqR7/2/

var testObj = (function() {
    var data = {__proto__:null}; // 100% private

    return new Class({
        get: function(key) {
            return data[key] || null;
        },
        set: function(key, value) {
            data[key] = value;
        },
        remove: function(key) {
            delete data[key];
        },
        otherMethod: function() {
            alert(this.get("foo"));
        }
    });
});


var foo = new new testObj();
var bar = new new testObj();

foo.set("bar", "banana");
console.log(foo.get("bar")); // banana!
console.log(bar.get("bar")); // undefined.
bar.set("bar", "apple");
console.info(foo.get("bar"), bar.get("bar")); // banana apple

编辑为什么会这样...

我对 mootools 的依赖意味着我对原生 js 原型的理解还有待改进,因为它抽象了你必须直接处理这个问题,但是..

在模式一中,您定义并运行该函数,该函数创建原型并设置data- 一个单一实例。data然后,您可以使用已设置的“实时”原型创建新功能。

在模式二中,为每个实例创建和引用一个全新的原型,彼此独立。您的函数返回一个带有原型类的新函数......因此实际上new Class({});new new <var>()创建并实例化该类。

为了更好地理解这一点,也许你可以先这样写 - 一种用于创建和实例化未被重用的类的足够常见的模式 - 这将更有意义:

new (new Class({
    initialize: function() {
        alert("hi");
    }
}))();

反过来可以这样写(如果保存到变量中):

var foo = new Class({
    initialize: function() {
        alert("hi");
    }
});

new foo();

我希望这是有道理的,我不是解释的最佳人...

于 2011-05-23T23:56:29.950 回答