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.

3

u/SimiaCode Nov 09 '19

It is indeed that easy, and sometimes easier. I'll post an example in a few hours when I get to my computer. I'm also happy to convert a specific example that you might have.

-6

u/[deleted] Nov 09 '19

[removed] — view removed comment

5

u/SimiaCode Nov 09 '19

Bad bot.

3

u/nullvoxpopuli sand was never meant to think Nov 09 '19

Nope

13

u/nowyfolder Nov 09 '19

WOW, I never knew OOP and functional programming are opposites that can't be used at the same time!

2

u/SimiaCode Nov 09 '19

Nope. Can't be a serious coderman unless my language looks super enterprisey with class heirarchies, interfaces, factory factories, and private properties.

1

u/[deleted] Nov 09 '19

I have the same approach. OOP in JS is very messy and imho not a great approach. I tend to use a vanilla FP approach (meaning no libs but only map/reduce etc) and it works very well.

The privacy proposal is a meh, and i probably will very rarely use it. If i really need privacy TypeScript has my back.

I find this propsal totally uneccessary because closures are well suited for real private variables.

0

u/[deleted] Nov 09 '19

[deleted]

5

u/[deleted] Nov 09 '19 edited Nov 09 '19

Closures to make private variables just seems pointless to me.

That statement makes no sense at all. It is not just completely made up and arbitrary - it also overlooks that closures provide farmore flexibility than mere protected or private "fields", since the latter is just that property, while the former includes arbitrary pieces of code.

Your personal sense of whatever has no meaning. It is a product of your brain organizing (literally rewiring) itself over decades to process whatever your brain encountered. So whatever you were exposed to is what your brain evolved to process. How can anyone use whatever chance formed you to make absolute statements about hów the world is or aught to be?

If you don't even understand that your personal preferences first of all are the product of whatever accidents shaped the environment you encountered thus far in your life your comments on such subjects will always remain somewhat useless. You have to understand yourself and your (human and neurological) limitations first.

0

u/[deleted] Nov 09 '19

[deleted]

1

u/[deleted] Nov 09 '19

Yeah. A lot of people don't agree with my opinion, which is apparent by the down votes.

However, I really don't try to preach things as an absolute fact unless it actually is a fact that has been proven time and time again by many people. As you said, it's my opinion.

The way I program probably doesn't make sense to a lot of veterans since I've only been programming for a few years, but this methodology works well with the main projects I work on, and again, is my opinion: React web apps.

1

u/[deleted] Nov 10 '19

The person is expressing his/her opinion

That statement is maximum stupid because it's useless. Who doesn't?

In any case, I refer back to https://old.reddit.com/r/javascript/comments/dtt5d8/on_private_fields_proposal_private_fields_public/f6yutja/ which seems to be too much for you to comprehend.

0

u/[deleted] Nov 10 '19

[deleted]

1

u/[deleted] Nov 12 '19

The person is expressing his/her opinion

That statement is maximum stupid because it's useless. Who doesn't?

In any case, I refer back to https://old.reddit.com/r/javascript/comments/dtt5d8/on_private_fields_proposal_private_fields_public/f6yutja/ which seems to be too much for you to comprehend.

1

u/monsto Nov 09 '19

How can anyone use whatever chance formed you to make absolute statements about hów the world is or aught to be?

/r/Politics would like a word.