0

First things first, this may be primary opinion based, so I'm looking for technical reasons and commonly accepted best practices to do one or the other thing.

If I write a "Class" and create a member of it, I can access the instance properties like this

function MyClass(){

  var self = this;

  self.foo = 'bar'

}   

var instance = new MyClass();

instance.foo = '420';

I could, however, also create a method to set the property, so that there is no knowledge about the internal structure of the class needed

MyClass.prototype.setFoo = function(prop){
  this.foo = prop;
};

Considering professional development, is there a reason to prefer one variant over the other?

Wottensprels
  • 3,307
  • 2
  • 29
  • 38
  • 1
    Basically the list from [why use getters/setters](http://stackoverflow.com/q/1568091/1048572) is valid for JS as well. – Bergi May 21 '14 at 09:15

2 Answers2

5

Here's a good example of how to use a private correctly - using scopes(in your example I can set\access foo outside the function without going through the set\get functions):

function MyClass(){
    var foo = 'bar'; // private because of "scope"

    this.setfoo  = function(f) {
        foo = f;
    }

    this.getfoo = function() {
        return foo;
    }
}   

var instance = new MyClass();
console.log(instance.foo); // undefined
console.log(instance.getfoo()); // bar
instance.setfoo('a');
console.log(instance.getfoo()); // a

Here's a Fiddle

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • Thank you. This sample leads me to a related question: Why are you implementing those setter methods as static ones instead of adding them to the prototype? – Wottensprels May 21 '14 at 06:43
  • 1
    @Sprotte Because a prototype function wouldn't have access to the privately scoped variable. – deceze May 21 '14 at 07:03
  • When you make 'private' members with closures you need separate method instances for each object instance. Each method instance will only see it's own object's private vars. – Vatev May 21 '14 at 07:03
  • 1
    Here's a good stack post that will give you answers: http://stackoverflow.com/questions/436120/javascript-accessing-private-member-variables-from-prototype-defined-functions – Amir Popovich May 21 '14 at 07:28
0

I suggest you read Nicholas Zakas's Professional Java Script for Web Developpers, starting page 90. He explains quite clearly the different patterns (Factory vs Constructor vs Prototype) to create a 'Class' in Java Script.

I got all the info I needed when I tried to create some classes a while ago. I hope you find what need :)

The problem with Amiros's suggestion is that, as Nicholas Zakas explains, "every time the myClass() function is called, a new function is created called setFoo(), meaning that every object has its own version of setFoo() when, in reality, each object should share the same function.

For example,

class1 = new MyClass();
class2 = new MyClass();
console.log(class1.setFoo == class2.setFoo); // false
Waldo Jeffers
  • 2,289
  • 1
  • 15
  • 19
  • Thank you, I'll give it a try! The problem with the prototype pattern is, as Amiros pointed out, that properties will still be editable from outside. Now I can't decide what way to go :/ – Wottensprels May 21 '14 at 08:41
  • 1
    What does Nicholas Zakas say about setter/getter methods? OP did not ask about classes. – Bergi May 21 '14 at 09:06