0

Below in my code example there is an example of a getter and setter is this the proper way of using them in Javascript?

Question: Is this how to use a getter and setter in Javascript?

Code:

<body>
<p>Object</p>

<script>
function Car( model, year, miles ) {
  this.model;
  this.year = year;
  this.miles = miles;

  this.setmodel = function (m) {
    if (do some checks here) {
       this.model = m;
    }
  };

  this.getmodel = function () {
    return model;
  };  

  this.toString = function () {
    return this.model + " has done " + this.miles + " miles";
  };
}

Car.prototype.toAnotherString = function () {
   return this.model + " has done " + this.miles + " miles";
};

var civic  = new Car( "Honda Civic", 2009, 20000 );
var mondeo = new Car( "Ford Mondeo", 2010, 5000 );

console.log( civic.toString() );
console.log( mondeo.toString() );
console.log( civic.toAnotherString() );
console.log( mondeo.toAnotherString() );

alert(civic.toString());
</script>   
</body>
</html>
5
  • 2
    your setter doesn't do anything. Commented Apr 1, 2014 at 19:38
  • You need to call your setter and getter functions from somewhere. Consider making them self calling. Commented Apr 1, 2014 at 19:40
  • 1
    For heaven's sake, why? What does this gain you in Javascript? Commented Apr 1, 2014 at 19:41
  • How do you make them self calling. Your right in Javascript it probably doesn't matter I am just trying to cover all of the basis in learning Javascript. From what I have read a person would not do this probably in real with a web page. Commented Apr 1, 2014 at 20:15
  • I know they are not called from anywhere. Commented Apr 1, 2014 at 20:16

2 Answers 2

3

No, that is not correct. Your setter needs to... set the value, and your getter needs to get the value...

  this.setmodel = function (m) {
    this.model = m;
  };

  this.getmodel = function () {
    return this.model;
  }; 

Seems just a little pointless though since that property is directly available.

var civic  = new Car( "Honda Civic", 2009, 20000 );
civic.model = "Some model";

Setters and Getters are typically used for private variables that are purposely not publicly available, or for transforming data (such as your toString method)

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

4 Comments

+1 for "pointless". This does not help encapsulate anything. The underlying properties are no less exposed by adding g/setters. They just add to the weight of the objects, or at least to their prototypes.
How can you make them private or is that even possible in javascript.
Is there really an reason in Javascript to have a getter or setter.
By declaring a variable inside the constructor and not setting it as a property on this.
0

Yes you can do that, if you fix the setter to this.model = m;

and the getter to return this.model;

It isn't really necessary though. Unless you have special operations you need to perform or special validation to handle on incoming values, it just takes up extra space.

Comments

Your Answer

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