Reputation: 3578
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?
Upvotes: 8
Views: 4028
Reputation: 76
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;
});
Upvotes: 2
Reputation: 21
"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());
Upvotes: 1
Reputation: 61327
In 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);
Upvotes: 11
Reputation: 146350
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"
Upvotes: 3