2

やあみんな、私が今持っているのはこれです:

var Human=function(){
  this._a=Math.random();
};
(function() {
  var before_get = function(human) {
  };
  var before_set = function(human, v) {
  };
  Human.prototype={
    get A(){
      before_get(this);
      return this._a;
    },
    set A(v){
      before_set(this, v);
      this._a=v;
    }
  };
})();
alert(new Human().A); // test
alert(new Human().A); // test

プロトタイプ以外の場所に変数_aを公開したくないことを除いて、すべて問題ありません。いくつか検索を行ったところ、それは不可能であることがわかったので、通常はそのままにしておくのでしょうか(つまり、これらの_a変数をそのままにしておくのか、それともより良い解決策があるのでしょうか)。

4

2 に答える 2

3

JavaScript にはプライベートなどというものがないため、これは達成できません。一般に、他の C#/Java のようなジェネリック プロパティやセッター/ゲッターはありません。

使用できるパターンは、プロトタイプではなくクロージャーです。

var Human = function() {
    var a = Math.random();
    var o = {};
    Object.defineProperties(o, {
        "A": {
             "get": function() {
                 return a;
             }, 
             "set": function(val) {
                 a = val;
             }
        }
    });
    return o;
}

ただし、一般に、プロトタイプにプロパティを書き込むべきではありません。プロトタイプにはメソッドが含まれている必要があります。

クリーンアップする唯一の方法this._aは次のとおりです

var Human = (function() {
  var Human=function(){
    this._a=Math.random();
  };
  var before_get = function(human) {
  };
  var before_set = function(human, v) {
  };
  Human.prototype={
    getA(){
      before_get(this);
      return this._a;
    },
    setA(v){
      before_set(this, v);
      this._a=v;
    }
  };
  return function(args) {
     var h = new Human(args);
     return {
       getA: h.getA.bind(h),
       setA: h.setA.bind(h)
     }
  }
})();
于 2011-04-20T17:58:03.597 に答える
1

これは、プロトタイプの「継承」を使用してプライベート/静的変数のようなものを作成する方法です。秘訣は、コンストラクター内でプロトタイプメソッドを定義することです(1回)。価格はあなたが実行しなければならないゲッター/セッターです。利益は単純さと真のプロトタイプソリューションです(結局のところ、これは獣の本質です)。ところで、ここでは「getter / setter」を1回だけ作成し、そこから作成するすべての999(999)インスタンスに存在します。

function Human() {
    var  a = 'We are all Human',
         o = {}
         proto = Human.prototype;
    if (!proto.A) {
       //uncomment and create a few instances to see
       //this is only executed once
       //console.log('adding prototypes');
       proto.A = function() {return a;};
       proto.set = function(val) {a = val || a;};
       proto.setName = function(val) {this.name = val || ''};
       proto.getName = function(){
           if (!this.name) {this.setName('no name yet');}
           return this.name;};
    }
}

var Pete = new Human,
    Jean = new Human;

console.log(Pete.A()+':'+Jean.A()); 
      //|=> We are all Human:We are all Human
Pete.set(Pete.A()+' except Jean'));
console.log(Pete.A()+':'+Jean.A()); 
      //|=> We are all Human except Jean:We are all Human except Jean
Pete.setName('Hi, I am Pete. '+Pete.A());
Jean.setName('Hi, I am Jean. ' +Jean.A()+ '. Damn! thats me!');
console.log(Pete.name); 
      //|=> Hi, I am Pete. We are all Human except Jean
console.log(Jean.name); 
      //|=> Hi, I am Jean. We are all Human except Jean. Damn! thats me!

誰でも他の何かをに割り当てることができることを理解する必要がありますHuman.prototype.A。しかし、コンストラクターの外部でそれを行うと、クロージャー、つまりa、は使用できなくなります。

于 2011-04-20T19:27:31.547 に答える