This question already has an answer here:
- Private properties in JavaScript ES6 classes 34 answers
I use Traceur Compiler to have advantage with ES6 features now.
I want to implement this stuff from ES5:
function Animal() {
var self = this,
sayHi;
sayHi = function() {
self.hi();
};
this.hi = function() {/* ... */}
}
Currently traceur does not support private
and public
keywords (from harmony). And ES6 class syntax does not allow to use simple var
(or let
) statements in class body.
The only way that I am find is to simulate privates before class declaration. Something like:
var sayHi = function() {
// ... do stuff
};
class Animal {
...
It is better then nothing but as expected you can not pass correct this
to private method without apply
-ing or bind
-ing it every time.
So, is there any possibility to use private data in ES6 class compatible with traceur compiler?
You can always use normal functions:
As Marcelo Lazaroni has already said,
But his example didn't show how the private method could access members of the instance of the class. Max shows us some good examples of how access instance members through binding or the alternative of using a lambda method in the constructor, but I would like to add one more simple way of doing it: passing the instance as a parameter to the private method. Doing it this way would lead Max's MyClass to look like this:
Which way you do it really comes down to personal preference.
Have you considered using factory functions? They usually are a much better alternative to classes or constructor functions in Javascript. Here is an example of how it works:
Thanks to closures you have access to all private functions and variabels inside the returned object, but you can not access them from outside.
Although currently there is no way to declare a method or property as private, ES6 modules are not in the global namespace. Therefore, anything that you declare in your module and do not export will not be available to any other part of your program, but will still be available to your module during run time. Thus, you have private properties and methods :)
Here is an example (in
test.js
file)In another file
You can use Symbol
P.S. alexpods is not correct. he get protect rather than private, since inheritance is a name conflict
Actually you can use
var say = String(Math.random())
instead SymbolIN ES6:
There are no
private
,public
orprotected
keywords in current ECMAScript 6 specification.So Traceur does not support
private
andpublic
. 6to5 (currently it's called "Babel") realizes this proposal for experimental purpose (see also this discussion). But it's just proposal, after all.So for now you can just simulate private properties through
WeakMap
(see here). Another alternative isSymbol
- but it doesn't provide actual privacy as the property can be easily accessed throughObject.getOwnPropertySymbols
.IMHO the best solution at this time - just use pseudo privacy. If you frequently use
apply
orcall
with your method, then this method is very object specific. So it's worth to declare it in your class just with underscore prefix: