2012-07-17 56 views
7

在Java編程語言中,專用關鍵字用於數據隱藏 - 標記爲專用的字段或方法在類或子類之外不可見。隱藏在Javascript中的數據

javascript是如何實現的?

+6

私人領域是通過重大的受虐行爲實現的。 – biziclop 2012-07-17 15:29:21

+1

與大多數JavaScript相關的東西一樣,道格拉斯克羅克福德在該語言中有一些有趣的[關於私人成員的想法](http://www.crockford.com/javascript/private.html)。 – maerics 2012-07-17 15:29:44

回答

8

在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); 
+0

偉大的工程和由道格拉斯克羅克福德本人推薦:) – awendt 2012-07-17 15:29:40

+0

你可以提供一些解釋,以什麼發生在你的代碼,以及這是如何涵蓋數據隱藏在JavaScript - 謝謝 – oneiros 2012-07-17 17:10:48

+1

查找「JavaScript閉包」,以瞭解如何保持數據遠離公共範圍。另一個函數內的任何函數都會創建一個閉包。內部函數可以「記住它周圍的東西」。上面的例子演示了一個函數EXECUT IMYEDIATELY with(),從而返回一些內部引用到名爲「testModule」的新對象。 – Billbad 2012-07-17 19:42:22

1

這一切都是通過範圍界定來實現的。

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" 
0
"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()); 
1

下面是一個簡單的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; 

    });