回答
在JavaScript中标准的方法是使用模块模式如下所示..
var testModule = (function() {
var myPrivateVar = 0;
var myPrivateMethod = function (someText) {
console.log(someText);
};
return {
myPublicVar: "foo",
myPublicFunction: function (bar) {
myPrivateVar++;
myPrivateMethod(bar);
}
};
})();
用法:在上面的代码被返回的对象,其包含一个变量(myPublicVar)和功能(myPublicFunction)。在此函数内部,您可以访问内部变量(myPrivateVar)和内部函数(myPrivateMethod),但不能从外部访问。
var mod = new testModule();
mod.myPublicFunction(param);
伟大的工程和由道格拉斯克罗克福德本人推荐:) – awendt 2012-07-17 15:29:40
你可以提供一些解释,以什么发生在你的代码,以及这是如何涵盖数据隐藏在JavaScript - 谢谢 – oneiros 2012-07-17 17:10:48
查找“JavaScript闭包”,以了解如何保持数据远离公共范围。另一个函数内的任何函数都会创建一个闭包。内部函数可以“记住它周围的东西”。上面的例子演示了一个函数EXECUT IMYEDIATELY with(),从而返回一些内部引用到名为“testModule”的新对象。 – Billbad 2012-07-17 19:42:22
这一切都是通过范围界定来实现的。
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"
"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());
下面是一个简单的API,你可能会喜欢。它有三个功能:Key(一个构造函数),KeepSecret,FetchSecret。
你可以做的是将一个具有秘密守护者的对象作为一个属性。然后对象可以“随身携带”数据,但是访问对象但不知道密钥的代码无法访问隐藏的数据。
/**
* 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;
});
- 1. 的Javascript提示 - 数据隐藏在IE
- 2. 隐藏数组在javascript中的对象
- 3. 如何隐藏JavaScript中的单独数组中的JSON数据?
- 4. 在JavaScript中隐藏div类?
- 5. 在javascript中显示/隐藏
- 6. 隐藏在cassandra中的列式数据
- 7. 隐藏我的哈希数字在javascript
- 8. 隐藏Javascript中的按钮
- 9. Javascript函数默认隐藏
- 10. 在jQuery数据表中隐藏列
- 11. 在Laravel中隐藏敏感数据
- 12. 如何在Angular2中隐藏数据?
- 13. 将数据隐藏在mp3文件中
- 14. JavaScript隐藏DIV
- 15. javascript隐藏divs
- 16. 在javascript中显示隐藏的div?
- 17. 如何隐藏在JavaScript中的面板
- 18. Javascript:将数据加载到隐藏的表单元素中
- 19. 隐藏jQuery数据表中的列?
- 20. 隐藏数据表中的列if if
- 21. 隐藏ASP.NET动态数据中的列
- 22. 隐藏行中的数据 - Excel VBA
- 23. 隐藏网址中的数据
- 24. 如何隐藏在JavaScript
- 25. 设定值:隐藏在JavaScript
- 26. Javascript中隐藏控制Repeater
- 27. Javascript - 隐藏分区
- 28. JavaScript显示/隐藏
- 29. Javascript隐藏DIV HTML
- 30. Javascript onclick隐藏div
私人领域是通过重大的受虐行为实现的。 – biziclop 2012-07-17 15:29:21
与大多数JavaScript相关的东西一样,道格拉斯克罗克福德在该语言中有一些有趣的[关于私人成员的想法](http://www.crockford.com/javascript/private.html)。 – maerics 2012-07-17 15:29:44