Skip to main content
added ES6 example
Source Link
Scott Rippey
  • 15.8k
  • 5
  • 73
  • 88

Update: With ES6, there is a better way:

Long story short, you can use the new Symbol to create private fields.
Here's a great description: https://curiosity-driven.org/private-properties-in-javascript

Example:

var Person = (function() { // Only Person can access nameSymbol var nameSymbol = Symbol('name'); function Person(name) { this[nameSymbol] = name; } Person.prototype.getName = function() { return this[nameSymbol]; }; return Person; }()); 

For all modern browsers with ES5:

You can use just Closures

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

Or you can use just Prototypes

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

Don't bother mixing Closures with Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

When you use a closure to access a private variable, prototype methods cannot access the variable. So, you have to expose the closure onto this, which means that you're exposing it publicly one way or another. There's very little to gain with this approach.

Which do I choose?

For really simple objects, just use a plain object with closures.

If you need prototypal inheritance -- for inheritance, performance, etc. -- then stick with the "_private" naming convention, and don't bother with closures.

I don't understand why JS developers try SO hard to make fields truly private.

Update: With ES6, there is a better way:

Long story short, you can use the new Symbol to create private fields.
https://curiosity-driven.org/private-properties-in-javascript

Example:

For all modern browsers with ES5:

You can use just Closures

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

Or you can use just Prototypes

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

Don't bother mixing Closures with Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

When you use a closure to access a private variable, prototype methods cannot access the variable. So, you have to expose the closure onto this, which means that you're exposing it publicly one way or another. There's very little to gain with this approach.

Which do I choose?

For really simple objects, just use a plain object with closures.

If you need prototypal inheritance -- for inheritance, performance, etc. -- then stick with the "_private" naming convention, and don't bother with closures.

I don't understand why JS developers try SO hard to make fields truly private.

Update: With ES6, there is a better way:

Long story short, you can use the new Symbol to create private fields.
Here's a great description: https://curiosity-driven.org/private-properties-in-javascript

Example:

var Person = (function() { // Only Person can access nameSymbol var nameSymbol = Symbol('name'); function Person(name) { this[nameSymbol] = name; } Person.prototype.getName = function() { return this[nameSymbol]; }; return Person; }()); 

For all modern browsers with ES5:

You can use just Closures

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

Or you can use just Prototypes

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

Don't bother mixing Closures with Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

When you use a closure to access a private variable, prototype methods cannot access the variable. So, you have to expose the closure onto this, which means that you're exposing it publicly one way or another. There's very little to gain with this approach.

Which do I choose?

For really simple objects, just use a plain object with closures.

If you need prototypal inheritance -- for inheritance, performance, etc. -- then stick with the "_private" naming convention, and don't bother with closures.

I don't understand why JS developers try SO hard to make fields truly private.

organized my answer so it's a little easier to read
Source Link
Scott Rippey
  • 15.8k
  • 5
  • 73
  • 88

Update: With ES6, there is a better way:

Long story short, you can use the new Symbol to create private fields.
https://curiosity-driven.org/private-properties-in-javascript

Example:

For all modern browsers with ES5:

Don't bother mixing Closures with Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

Although you clearly already understand closures, private variables, and prototypal inheritance, I'd like to describe them to illustrate my point.

UseYou can use just Closures for Private and Public

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

Use Prototype for Private and PublicOr you can use just Prototypes

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

Don't usebother mixing Closures for Private and Prototype for Publicwith Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

When you use a closure to createaccess a private variable, youprototype methods cannot access it from a prototypal method unless it's exposed through the this variable. Most solutions, thereforeSo, justyou have to expose the variable by a methodclosure onto this, which means that you're exposing it publicly one way or another. There's very little to gain with this approach.

So, which one toWhich do I choose?

I think using prototypal inheritance makes the most sense, makes debugging easierFor really simple objects, provides transparencyjust use a plain object with closures.

If you need prototypal inheritance -- for inheritance, could improve performance, and so that's what I usually useetc.
Stick to conventions for _private fields-- then stick with the "_private" naming convention, and everything goes greatdon't bother with closures.
And 

I don't understand why JS developers try SO hard to make fields truly private.

Update: With ES6, there is a better way:

Long story short, you can use the new Symbol to create private fields.
https://curiosity-driven.org/private-properties-in-javascript

For all modern browsers with ES5:

Don't bother mixing Closures with Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

Although you clearly already understand closures, private variables, and prototypal inheritance, I'd like to describe them to illustrate my point.

Use Closures for Private and Public

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

Use Prototype for Private and Public

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

Don't use Closures for Private and Prototype for Public

When you use a closure to create a private variable, you cannot access it from a prototypal method unless it's exposed through the this variable. Most solutions, therefore, just expose the variable by a method, which means that you're exposing it publicly one way or another.

So, which one to choose?

I think using prototypal inheritance makes the most sense, makes debugging easier, provides transparency, could improve performance, and so that's what I usually use.
Stick to conventions for _private fields and everything goes great.
And I don't understand why JS developers try SO hard to make fields truly private.

Update: With ES6, there is a better way:

Long story short, you can use the new Symbol to create private fields.
https://curiosity-driven.org/private-properties-in-javascript

Example:

For all modern browsers with ES5:

You can use just Closures

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

Or you can use just Prototypes

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

Don't bother mixing Closures with Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

When you use a closure to access a private variable, prototype methods cannot access the variable. So, you have to expose the closure onto this, which means that you're exposing it publicly one way or another. There's very little to gain with this approach.

Which do I choose?

For really simple objects, just use a plain object with closures.

If you need prototypal inheritance -- for inheritance, performance, etc. -- then stick with the "_private" naming convention, and don't bother with closures. 

I don't understand why JS developers try SO hard to make fields truly private.

added 281 characters in body
Source Link
Scott Rippey
  • 15.8k
  • 5
  • 73
  • 88

Update: With ES6, there is a better way:

Long story short, you can use the new Symbol to create private fields.
https://curiosity-driven.org/private-properties-in-javascript

For all modern browsers with ES5:

Don't bother mixing Closures with Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

Although you clearly already understand closures, private variables, and prototypal inheritance, I'd like to describe them to illustrate my point.

ClosuresUse Closures for Private and Public

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

PrototypeUse Prototype for Private and Public

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

UsingDon't use Closures for Private and prototypePrototype for Public

When you use a closure to create a private variable, you cannot access it from a prototypal method unless it's exposed through the this variable. Most solutions, therefore, just expose the variable by a method, which means that you're exposing it publicly one way or another.

So, which one to choose?

I think using prototypal inheritance makes the most sense, makes debugging easier, provides transparency, could improve performance, and so that's what I usually use.
Stick to conventions for _private fields and everything goes great.
And I just don't understand why JS developers try SO hard to make fields truly private.

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

Although you clearly already understand closures, private variables, and prototypal inheritance, I'd like to describe them to illustrate my point.

Closures for Private and Public

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

Prototype for Private and Public

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

Using Closures for Private and prototype for Public

When you use a closure to create a private variable, you cannot access it from a prototypal method unless it's exposed through the this variable. Most solutions, therefore, just expose the variable by a method, which means that you're exposing it publicly one way or another.

So, which one to choose?

I think using prototypal inheritance makes the most sense, makes debugging easier, provides transparency, could improve performance, and so that's what I usually use.
Stick to conventions for _private fields and everything goes great.
And I just don't understand why JS developers try SO hard to make fields truly private.

Update: With ES6, there is a better way:

Long story short, you can use the new Symbol to create private fields.
https://curiosity-driven.org/private-properties-in-javascript

For all modern browsers with ES5:

Don't bother mixing Closures with Prototypes

I think you shouldn't mix closure variables with prototype methods. You should use one or the other.

Although you clearly already understand closures, private variables, and prototypal inheritance, I'd like to describe them to illustrate my point.

Use Closures for Private and Public

The simplest way to construct objects is to avoid prototypal inheritance altogether. Just define the private variables and public functions within the closure, and all public methods will have private access to the variables.

Use Prototype for Private and Public

In JavaScript, prototypal inheritance is primarily an optimization. It allows multiple instances to share prototype methods, rather than each instance having its own methods.
The drawback is that this is the only thing that's different each time a prototypal function is called.
Therefore, any private fields must be accessible through this, which means they're going to be public. So we just stick to naming conventions for _private fields.

Don't use Closures for Private and Prototype for Public

When you use a closure to create a private variable, you cannot access it from a prototypal method unless it's exposed through the this variable. Most solutions, therefore, just expose the variable by a method, which means that you're exposing it publicly one way or another.

So, which one to choose?

I think using prototypal inheritance makes the most sense, makes debugging easier, provides transparency, could improve performance, and so that's what I usually use.
Stick to conventions for _private fields and everything goes great.
And I don't understand why JS developers try SO hard to make fields truly private.

added 54 characters in body
Source Link
Scott Rippey
  • 15.8k
  • 5
  • 73
  • 88
Loading
Source Link
Scott Rippey
  • 15.8k
  • 5
  • 73
  • 88
Loading