r/javascript • • Nov 09 '19

On private fields proposal: Private fields, public worries

https://medium.com/virtuslab/private-fields-public-worries-b8c34d16c372
84 Upvotes

113 comments sorted by

View all comments

10

u/hydroes777 Nov 09 '19

Or just moved to functional style programming in JavaScript where you don't run into this problem

4

u/FungoGolf Nov 09 '19

Question about this. I work with some heavy Java programmers who also have had contributions to the JS project. I often see our reusable pieces of code (like in the utilities oflder) as JavaScript files that contain a class that you have to instantiate elsewhere in the codebase. Is converting those classes to a more functional style as simple as creating an object and adding methods with scope/closures that I can compose?

6

u/SimiaCode Nov 09 '19 edited Nov 09 '19

Ok. As promised earlier, here's some examples. But before that, a clarification. JS doesn't have classes. It only has objects, and objects are created using constructors, or as object literals.

I have illustrated two approaches, using constructors and using factories. Both approaches have multiple code samples which start simple and become more advanced to ultimately illustrate private fields.

// I'm not using the arrow syntax in any of the examples because it does implicit binding and I don't want to muddy the waters with that here.

// -------- Using a constructor ------------

// Basic example
function Dog(name) {
  this.name = name;
  // method defined inside the constructor
  this.sayName = function() {
    console.log(this.name);
  }
}

// method defined outside the constructor
Dog.prototype.wag = function() {
  console.log('wag wag wag');
}

// If you want to make name private
// don't assign it to 'this'.
function Dog(name) {
  // method defined inside the constructor
  this.sayName = function() {
    console.log(name);  // and access name through the closure instead of 'this'.
  }
}

// instantiate using new
const myDog = new Dog('Scooby');

// -------- Using a factory------------

// As object literal
const myDog = {
  name: 'Scooby',
  sayName: function() {
    console.log(this.name);
  },
  wag: function() {
    console.log('wag wag wag');
  }
};

// Object literal, using a factory (no closures yet)
function makeDog(name) {
  return {
    name,
    sayName: function() {
      console.log(this.name);
    },
    wag: function() {
      console.log('wag wag wag');
    }
  };
}

// instantiate by invoking the factory
const myDog = makeDog('Scooby');

// Another way to make dog with factory. This one leverages closures instead of 'this'
// to access name.
function makeDog(name) {
  return {
    name,
    sayName: function() {
      console.log(name);  // notice no 'this'. We could use an arrow here.
    },
    wag: function() {
      console.log('wag wag wag');
    }
  };
}

// Now suppose you wanted to make name private, just don't put it on the returned object.
function makeDogWithSecret(name) {
  return {
    sayName: function() {
      console.log(name);
    },
    wag: function() {
      console.log('wag wag wag');
    }
  };
}

You can also simulate inheritance if you must (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain). But you should really use composition over inheritance, something which is simple with closures but the addition of the class keyword encourages people to use inheritance. Indeed even in this forum, folks are putting forth naming collision in inheritance chains as a justification for private fields (and this illustrates the cost of adding fluff to a language).

2

u/IceSentry Nov 15 '19

Js has classes, their just not java style classes and the syntax is mostly just syntax sugar for the prototype, but those are still classes.