最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oop - Getters and setters in javascript - Stack Overflow

programmeradmin0浏览0评论

I am starting with javascript and specially with the OOP pattern.

My question is simple. In a setter, is there a way to keep the same name for the parameter and the private class variable? I have looked everywhere but couldn't find anyone mentioning it, just examples with different var names. I am very picky with my code and I have having to give it two different names.

Taking the example from /

function Field(val){
    this.value = val;
}
Field.prototype = {
    get value(){
        return this._value;
    },
    set value(val){
        this._value = val;
    }
};

you can see in the setter the parameter is val but the property is actually value. Since it is javascript I cannot simply do this.value=value because "this" would make it public. value=value would refer both to the parameter (and would be very weird). Is there really no way to do this? If not, is there any "best practice" for this situation? I guess underscore could be valid but I am just picky so just want to make sure there is no other way.

Thank you!

I am starting with javascript and specially with the OOP pattern.

My question is simple. In a setter, is there a way to keep the same name for the parameter and the private class variable? I have looked everywhere but couldn't find anyone mentioning it, just examples with different var names. I am very picky with my code and I have having to give it two different names.

Taking the example from http://ejohn/blog/javascript-getters-and-setters/

function Field(val){
    this.value = val;
}
Field.prototype = {
    get value(){
        return this._value;
    },
    set value(val){
        this._value = val;
    }
};

you can see in the setter the parameter is val but the property is actually value. Since it is javascript I cannot simply do this.value=value because "this" would make it public. value=value would refer both to the parameter (and would be very weird). Is there really no way to do this? If not, is there any "best practice" for this situation? I guess underscore could be valid but I am just picky so just want to make sure there is no other way.

Thank you!

Share Improve this question asked Apr 28, 2012 at 0:40 martinvigomartinvigo 1433 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can use closure to hide the variable.

function MyClass {
  var value;

  this.getValue = function() {
    return value;
  }

  this.setValue = function(val) {
    value = val;
  }

}

After the constructor MyClass finishes, the value field is unacessible, as it was scoped only to this constructor (function). So we may say that value is a private field. However, the methods getValue() and setValue() are publicly accessible from the constructed object and they keep reference to the variable scope of MyClass, thus they can still manipulate value.

Using closures:

(function(exports){

    exports.field = {};
    var _value = '123';

    exports.field.get = function(){
        return _value;
    }

    exports.field.set = function(val){
        _value = val;
    }

})(window);

console.log(field.get());

Here is a good tutorial on closures in JS.

发布评论

评论列表(0)

  1. 暂无评论