Neat alternatives to __defineGetter__?

2020-02-23 15:32发布

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().

3条回答
▲ chillily
2楼-- · 2020-02-23 15:36

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);
}
查看更多
疯言疯语
3楼-- · 2020-02-23 15:38

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
查看更多
聊天终结者
4楼-- · 2020-02-23 16:01

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.

查看更多
登录 后发表回答