Neat alternatives to __defineGetter__?

拜拜、爱过 提交于 2019-12-18 11:53:51

问题


Getters and setters are a beauty in VB.Net:

Get
    Return width
End Get
Set(ByVal value As Integer)
    width = value
End Set

In Javascript, this is probably what we would do:

function Test() {
    var width = 100;
    this.__defineGetter__("Width", function() {
        return width;
    });
    this.__defineSetter__("Width", function(value){
        width = value;
    });
}

It looks like a plate of spaghetti ransacked by a kuri. What are some neater alternatives we have?

Note: The new code should access the value using new Test().Width and not new Test().Width().


回答1:


With ES5 you'll be able to do:

function Test() {
  var a = 1;

  return {
    get A() { return a; },
    set A(v) { a = v; }
  };
}

The getter/setter functions can of course do anything you want them to.




回答2:


Here's a clean(er) alternative (also for older script engines):

function Test() {
  var a=1;
  return { A: { toString: function(){return a;} } };
}

alert(Test().A); //=> 1

Mind you, you can't use it to define private/static complex structures. You can only 'get' strings or numbers (so immutable variables) with this pattern. Maybe the pattern can be enhanced using json.

[edit] Using json, you can also create a getter this way for objects:

function Test() {
  var a=1,
  b = {foo:50, bar:100};

  return { 
          A: { toString: function(){return a;} } 
          foobar: { toString: function(){return JSON.stringify(b);}  } 
         };
}
var foobar = JSON.parse(Test().foobar);
alert(foobar.foo); //=> 50



回答3:


In Ecmascript5, the 'clean' (and standards compliant) way of doing this is with defineProperty.

function Test() {
    var a = 1;
    Object.defineProperty(this, "A", {get : function() { 
            return a;
        },  
        enumerable : true});
}

This assumes that you just want to see how to define a getter. If all you want to do is make instances of Test immutable (a good thing to do where you can), you should use freeze for that:

function Test() {
    this.a = 1;
    Object.freeze(this);
}


来源:https://stackoverflow.com/questions/5731454/neat-alternatives-to-definegetter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!