回答
在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