In the Java Programming language the private keyword is used for data hiding - a field or a method marked as private is not visible outside the classes or the subclasses.
How is that achieved in javascript?
In the Java Programming language the private keyword is used for data hiding - a field or a method marked as private is not visible outside the classes or the subclasses.
How is that achieved in javascript?
Share Improve this question edited Jul 17, 2012 at 15:45 Alberto De Caro 5,2139 gold badges50 silver badges75 bronze badges asked Jul 17, 2012 at 15:27 oneirosoneiros 3,57812 gold badges45 silver badges73 bronze badges 2- 6 Private fields are achieved via significant masochism. – biziclop Commented Jul 17, 2012 at 15:29
- 1 As with most things related to JavaScript, Douglas Crockford has some interesting ideas about private members in that language. – maerics Commented Jul 17, 2012 at 15:29
4 Answers
Reset to default 11In JavaScript standard way is to use Module Pattern as shown below..
var testModule = (function () {
var myPrivateVar = 0;
var myPrivateMethod = function (someText) {
console.log(someText);
};
return {
myPublicVar: "foo",
myPublicFunction: function (bar) {
myPrivateVar++;
myPrivateMethod(bar);
}
};
})();
Usage: In the above code an object is returned which contains a variable (myPublicVar) and a function(myPublicFunction). Inside this function you can access the inner variable (myPrivateVar) and inner function(myPrivateMethod) but not from outside.
var mod = new testModule();
mod.myPublicFunction(param);
This all achieved with scoping.
var MYCLASS = function(){
var priv_var = 0; //private var
this.addToVar = function(){
priv_var++;
}
this.showVar = function(){
return priv_var;
}
}
var mc = new MYCLASS;
mc.addTovar();
alert(mc.showVar()); //"1"
Here is a simple API that you might enjoy. It has three functions: Key (a constructor), KeepSecret, FetchSecret.
What you can do is to have an object with a secret-keeper as a property. The object can then "carry around" data, but code which accesses the object but does not know the key is unable to access the hidden data.
/**
* Example usage:
*
* Create a key
*
var mykey = Key();
*
* Keep a secret
*
var mykeeper = KeepSecret(mykey, 42);
*
* Fetch the secret
*
var answer = FetchSecret(mykey, mykeeper);
*
* 'answer' will then contain 42
*/
(function(Namespace, Code) { return Code(Namespace); })(
/* Choose the namespace for Key, KeepSecret, FetchSecret */
this,
function(Namespace) {
/*
* Simply so that we can use "Key" as both a type-name
* and a parameter-name
*/
var ikey;
/** Constructor for a key */
function Key() {
if (!(this instanceof Key))
return new Key();
}
/* Same key constructor */
ikey = Key;
/**
* Hide a secret using a key
*
* @param Key
* The key to lock away the secret with
*
* @param Secret
* The secret to be locked away
*
* @return
* A function which hides the secret inside
*/
function KeepSecret(Key, Secret) {
/* The function can access itself */
var closure;
if (!(Key instanceof ikey))
throw "KeepSecret: Invalid key";
closure = function(key) {
/* If we were not passed the key, authenticate */
if (key !== Key) {
Key.keeper = closure;
return;
}
/* The caller knew the key, so reveal the secret */
return Secret;
}
return closure;
}
/**
* Use a key and a function to reveal the secret that function keeps
*
* @param Key
* The key for unlocking the secret
*
* @param Keeper
* The function keeping the secret
*
* @return
* The secret, if the key unlocks it
*/
function FetchSecret(Key, Keeper) {
/* Tracks authentication */
var closure;
if (!(Key instanceof ikey) || !(Keeper instanceof Function))
throw "FetchSecret: Invalid parameter(s)";
/* Ask the keeper to authenticate */
Keeper();
/* Note the authenticated function */
closure = Key.keeper;
/* Clear the authentication */
delete Key.keeper;
/* Did the keeper prove that they know the key? */
if (closure !== Keeper)
/* No */
return;
/* They know the key. Show we know the key, too */
return closure(Key);
}
Namespace.Key = Key;
Namespace.KeepSecret = KeepSecret;
Namespace.FetchSecret = FetchSecret;
return true;
});
"use strict";
var Person = function (fName, lName) {
var firstName = fName || "AN", lastName = lName || "Other";
var fullName = function () {
return firstName + ' ' + lastName;
};
return {
setName: function (fName, lName) {
firstName = fName;
lastName = lName;
},
getFullName: function() {
return fullName();
}
};
}
var p = new Person("Your", "name");
console.log(p.getFullName());