5

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.org/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!

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Prototypes are nice though, and fast.
Closures are very interesting, thanks! Still, the question was about using the same variable name in the setter for the parameter and the class var but in you example you still have to use different ones. I am locking for a way to use the same name for both if it is posible. Something like in java would be this.variable-variable (both are called variable but the first one refers to the class variable and the second one to the param
@AlexWayne Sure, but you can't declare the "hidden" variable in the prototype... here, the trick is that a constructor is a function, which has a variable scope that remains in the memory after the function is finished. A prototype is just an object. Also, how are prototypes fast? They save memory, not time.
@yowie Of course, the name had to be changed because there would be no way to distinguish between the variable and the parameter. And as soon as you use this for the variable, the variable is not hidden. Now I can't think of a way to use the same name for them... But, there is a way of not mentioning the parameter name at all: this.setValue = function() { value = arguments[0]; };. The arguments object is defined inside all functions, keeping an array of the arguments passed to the function and a few other interesting things.
My question is answered now, there is no way for it but I could do what you suggested. Thank you @lmp
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.